Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

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)

like image 213
Fred Foo Avatar asked Oct 22 '25 14:10

Fred Foo


2 Answers

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.

like image 189
zzzz Avatar answered Oct 25 '25 10:10

zzzz


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.

like image 29
Fred Foo Avatar answered Oct 25 '25 10:10

Fred Foo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!