I just wanted to confirm my understanding of a typical project folder structure in a Go project, which follows the Go tool conventions.
If i'm writing a package called my-package
, which uses another 3rd party external package - other-package
. Then based on conventions, is this directory structure correct ?
+bin
+pkg
+src
|
--- my-package.com/..
|
--- external-package.com/..
The reason I ask this again after reading the docs is because unlike other language conventions, the user and 3rd party code are in the same top level folder.
A better way to organize a Go project is to put relevant Go code files into a subdirectory under the main directory for the project so that other parts of the project are able to find the APIs and use them. Keeping all source files under the same directory is not a very good idea, even though you can do it.
/cmd. This folder contains the main application entry point files for the project, with the directory name matching the name for the binary. So for example cmd/simple-service meaning that the binary we publish will be simple-service .
You can actually have Go projects outside of GOPATH. However, some tools do not work well or do not work at all in this kind of situation. For example, goimports , which formats your code and add missing imports, will not be able to find packages that are outside of GOPATH.
via irc #go-nuts
A user's machine will have a common $GOPATH/src
for all projects. This $GOPATH/src
could have multiple projects (user's, external packages etc.) there. The user would just commit the $GOPATH/src/mypackage
to their src repo.
If you want to have separate directories for your src, and 3rd party package source, then prepend another path to $GOPATH
. go get
will download the 3rd party packages to that path. go-get
downloads 3rd packages to the first path it finds in the array of paths specified by $GOPATH
.
Golang is so diferent to other languages with respect this topic, I can say that in my firts days with Golang was hard to understand this topic. I recommend you to have all the proyects on $GOPATH/src and dont have more that one $GOPATH.
On GOLANG we have to adapted to it, like sa http://golang.org/doc/code.html (Code organization)
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