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

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?

1 Like

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.