Fred Foo
Fred Foo

Reputation: 363497

What does the Go compiler mean by "no such file or directory"?

I'm trying to compile a Go program made up of multiple modules, like so:

// main.go
package main
import "mst"
// do something interesting involving minimum spanning trees

// src/mst/kruskal.go
import "disjsets"
// Kruskal's algorithm follows

// src/disjsets/disjsets.go
// implements disjoint sets with union-find

Now, when I run either go run main.go or go build after export GOPATH=. in the directory containing both main.go and src, it prints

# disjsets
open src/disjsets/disjsets.go: No such file or directory

I don't get this. The file is there as ls -l src/disjsets/disjsets.go confirms. How can this happen? Where should the disjsets.go file live if Go is to find it?

(Google Go 1.0.2)

Upvotes: 4

Views: 17674

Answers (2)

Fred Foo
Fred Foo

Reputation: 363497

Ok, this seems to solve it:

export GOPATH=`pwd`

Apparently, it needs to be an absolute path. I still find the error message very confusing, though.

Upvotes: 3

zzzz
zzzz

Reputation: 91193

I believe you should read, or re-read How to Write Go code

In short:

Set you GOPATH to somewhere and export it for good. Then put some package blah into directory

$GOPATH/src/foo/bar/baz/blah # (1)

or

$GOPATH/src/blah # (2)

or

$GOPATH/src/qux/blah # (3) etc.

Import blah into other packages as

import "foo/bar/baz/blah" // (1)

or

import "blah" // (2)

or

import "qux/blah" // (3)

The package in that directory will contain the package files. Say you have only one, blah.go. Then its location would be

$GOPATH/src/foo/bar/baz/blah/blah.go // (1)

$GOPATH/src/blah/blah.go // (2)

$GOPATH/src/qux/blah/blah.go // (3)

If the blah package source file is named, say proj.go instead, then

$GOPATH/src/foo/bar/baz/blah/proj.go // (1)

$GOPATH/src/blah/proj.go // (2)

$GOPATH/src/qux/blah/proj.go // (3)

But the import paths would be the same as in the previous case.

Upvotes: 8

Related Questions