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

Go

Go Problem Overview


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?

Go Solutions


Solution 1 - Go

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.

Solution 2 - Go

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.

Solution 3 - Go

Make sure that your package is installed in your $GOPATH directory or already inside your workspace/package.

For example: if your $GOPATH = "c:\go", make sure that the package inside C:\Go\src\pkgName

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTheTeaManView Question on Stackoverflow
Solution 1 - GoDenys SéguretView Answer on Stackoverflow
Solution 2 - GodkinzerView Answer on Stackoverflow
Solution 3 - GoMuhammad SolimanView Answer on Stackoverflow