Proper directory structure for a Go project?

I'm relatively new to Go, and recently I created a project that goes on GitHub. I tried to follow the manuals, but there is an urgent question about why my binaries end in src/ ?

My layout is this:

 ssm/ - Name of project LICENSE README.md src/ - Source Code files.go - All my source code is here. src - The compiled binary ends up here bin/ - Binaries 

I installed $GOPATH ~/Documents/Programming/Go/ . From my gopat I cannot type go build ssm , because it cannot find package . If I wrote a directory to a directory, it complains about it can't load package: package .: no Go source files .

I need to go into src and compile there, which means the binary is not in bin/ .

What am I doing wrong?

+6
source share
2 answers

See https://code.google.com/p/go-wiki/wiki/GithubCodeLayout

To be compatible with go get , your project package name must be fully sub-domain github.com:

 $GOPATH/ src/github.com/<user>/ssm/ .git LICENSE README.md files.go bin/ 

Note that the git repository base (.git) does not match the $GOPATH base.

In addition, go build <package> will output the compiled executable to the current directory. If you want exe to go to bin/ , use go install <package> .

+7
source

The code of your code that you can save in the workspace. The workspace contains many source files (git, hg, svm, etc.) . The Go tool understands the layout. We do not require a Makefile or build.xml here. The main catalog layout is everything. If in any case, if you are going to change the layout of the file, accordingly you need to change the assembly.

This is a general structure that you can follow.

$ GOPATH /

 src/ github.com/username/repo/ mypkg/ mysrc1.go mysrc2.go cmd/mycmd/ main.go bin/ mycmd 

And this is a standard workspace

$ GOPATH /

 bin/fixhub # installed binary pkg/darwin_amd64/ # compiled archives code.google.com/p/goauth2/oauth.a github.com/... src/ # source repositories code.google.com/p/goauth2/ .hg oauth # used by package go-github ... github.com/ golang/lint/... # used by package fixhub .git google/go-github/... # used by package fixhub .git dsymonds/fixhub/ .git client.go cmd/fixhub/fixhub.go # package main 

go get retrieves many repositories, while go install builds binary from them. Conveniently and easily go to development quickly. And everyone in the go community goes the same way. This puts src , bin and pkg in the home directory. And $ HOME / bin is already on our way before creating our workspace.

+2
source

Source: https://habr.com/ru/post/954961/


All Articles