Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

go-lang project folder structure convention

Tags:

go

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.

like image 947
Akshay Rawat Avatar asked Apr 10 '14 13:04

Akshay Rawat


People also ask

How do I structure my go project?

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.

What is CMD folder in Golang?

/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 .

Do go projects have to be in Gopath?

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.


2 Answers

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.

like image 143
Akshay Rawat Avatar answered Sep 28 '22 03:09

Akshay Rawat


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)

like image 24
Carlos Andrés García Avatar answered Sep 28 '22 04:09

Carlos Andrés García