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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With