78

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?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
TheTeaMan
  • 2,373
  • 3
  • 15
  • 13

3 Answers3

81

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.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 1
    Yes, but does package main qualify as an independent package? To my understanding, package main is a quirk of the language that provides the entry point (much like Java's classes that have public static main). – TheTeaMan Jan 19 '13 at 16:35
  • Yes, that's how it works. In practice I put in my "main" package very few code, only the one that can't be used in other programs. – Denys Séguret Jan 19 '13 at 16:39
  • 9
    That's quite annoying to be frank. I don't see a reason to separate the main entry point from the rest of the program. I will accept this answer. – TheTeaMan Jan 19 '13 at 16:42
  • When I make a program, I often have many small utilities using the same data/model/etc.. It's convenient to break everything in packages and have all programs very small and using those packages. BTW you can wait a little before accepting an answer, so that other users would have incentive to give you other (maybe better) answers. – Denys Séguret Jan 19 '13 at 16:44
  • if package is `main` it's a program. If package isn't `main` it's a library. If you put them in the same path and install that path, are you trying to install the program or the library? – Dustin Jan 20 '13 at 03:55
  • 2
    I'm not trying to install anything yet, just building the program. The program is dependent on both packages. I found out that putting the main package in `/src/my_prog/` and the my_prog package in `/src/my_prog/my_prog/` works. – TheTeaMan Jan 20 '13 at 10:52
21

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.

dkinzer
  • 32,179
  • 12
  • 66
  • 85
2

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

Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75