patterngoCritical
Organizing a multiple-file Go project
Viewed 0 times
projectmultipleorganizingfile
Problem
Note: this question is related to this one, but two years is a very long time in Go history.
What is the standard way to organize a Go project during development ?
My project is a single package
But then, I would like to test it during development so I need at least a file declaring the
How should I organize this ? Do I need to do
What is the standard way to organize a Go project during development ?
My project is a single package
mypack, so I guess I put all the .go files in a mypack directory.But then, I would like to test it during development so I need at least a file declaring the
main package, so that I can do go run trypack.goHow should I organize this ? Do I need to do
go install mypack each time I want to try it ?Solution
I would recommend reviewing this page on How to Write Go Code
It documents both how to structure your project in a
The structure suggested in that link in your question is a bit outdated, now with the release of Go 1. You no longer would need to place a
So your GOPATH might look like this:
Update: as of >= Go 1.11, the Module system is now a standard part of the tooling and the GOPATH concept is close to becoming obsolete.
It documents both how to structure your project in a
go build friendly way, and also how to write tests. Tests do not need to be a cmd using the main package. They can simply be TestX named functions as part of each package, and then go test will discover them.The structure suggested in that link in your question is a bit outdated, now with the release of Go 1. You no longer would need to place a
pkg directory under src. The only 3 spec-related directories are the 3 in the root of your GOPATH: bin, pkg, src . Underneath src, you can simply place your project mypack, and underneath that is all of your .go files including the mypack_test.gogo build will then build into the root level pkg and bin.So your GOPATH might look like this:
~/projects/
bin/
pkg/
src/
mypack/
foo.go
bar.go
mypack_test.goexport GOPATH=$HOME/projects$ go build mypack
$ go test mypackUpdate: as of >= Go 1.11, the Module system is now a standard part of the tooling and the GOPATH concept is close to becoming obsolete.
Code Snippets
~/projects/
bin/
pkg/
src/
mypack/
foo.go
bar.go
mypack_test.go$ go build mypack
$ go test mypackContext
Stack Overflow Q#9985559, score: 177
Revisions (0)
No revisions yet.