Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error "can't load package: package my_prog: found packages my_prog and main"

Tags:

go

In my GOPATH I have something like this:

/bin/ /pkg/ /src/ /src/my_prog/ /src/my_prog/main.go /src/my_prog/d_interface.go /src/my_prog/d_struct_that_implements_the_interface.go 

In main.go I have package main, in d_interface.go and d_struct_that_implements_the_interface.go I have package my_prog.

When I try to go build my_prog I get the following error:

can't load package: package my_prog: found packages my_prog (d_interface.go) and main (main.go) in C:\dev\Code\Go\src\my_prog 

Does this mean that any file that belongs to package main should go in its own folder? If so, what is the reason for this?

like image 863
TheTeaMan Avatar asked Jan 19 '13 16:01

TheTeaMan


2 Answers

Yes, each package must be defined in its own directory.

The source structure is defined in How to Write Go Code.

A package is a component that you can use in more than one program, that you can publish, import, get from an URL, etc. So it makes sense for it to have its own directory as much as a program can have a directory.

like image 75
Denys Séguret Avatar answered Sep 30 '22 18:09

Denys Séguret


Also, if all you are trying to do is break up the main.go file into multiple files, then just name the other files "package main" as long as you only define the main function in one of those files, you are good to go.

like image 36
dkinzer Avatar answered Sep 30 '22 18:09

dkinzer