This is a creation in Article, where the information may have evolved or changed.
Golang Project directory Structure
< golang_proj >
README
├─ AUTHORS
├─< bin ;
├─< Pkg ,
├─< src
The Golang project does not require any engineering documents, and a complete project will typically place the files or directories shown above in the project root directory . Their role is as follows:
√ README : A brief introduction to the objectives and main considerations of this project, which is usually the first time you should read this document.
√ LICENSE : The distribution agreement used in this project, which is usually owned by all open source projects.
√ src : src directory is used to store all source code.
√ pkg : The pkg directory is used to store the compiled package files without having to create them manually.
√ bin : The bin directory is used to store the compiled executable without having to create it manually.
src Directory
√ directory src is used to contain all the source code and is a mandatory rule for go tool.
√ in the construction process, thego tool's understanding of the package structure depends entirely on the directory structure below SRC .
√ The third-party source code downloaded by the go get command will be placed in the first path in the gopath environment variable.
As explained by the Calc instance mentioned in the Golang package, its directory structure begins as follows:
<calcproj>
├─<src>
├─<calc>
├─calc.go
├─<simplemath>
├─add.go
├─add_test.go
├─sqrt.go
├─sqrt_test.go
├─<bin>
├─<pkg>
At the command line, we use the go install simplemath command to package the Simplemath under the project path , which can be found in the pkg directory. SIMPLEMATH.A file.
If we want the path of the simplemath.a to have a namespace, that is, if you want to import it in the same way as import "Myns/simplemath", you need to adjust the directory structure to the following form:
<calcproj>
├─<src>
├─<calc>
├─calc.go
├─<simplemath>
├─<MyNS>
├─add.go
├─add_test.go
├─sqrt.go
├─sqrt_test.go
├─<bin>
├─<pkg>
Then on the command line, we use the go install myns/simplemath command to package the Simplemath under the project path, which can be found in the pkg directory simplemath.a file.
If, at the command line, we execute the go Install calc command under the project path, you can not only find the calc.exe executable file in the bin directory , but also the pkg directory Locate the simplemath.a package file below.
Bin Directory
√ The directory bin is used to store the compiled executable file.
Pkg Directory
√ The directory pkg is used to store the compiled package files.
Golang Multiple project management
• Programme One
The solution is to set up multiple gopath paths, adding paths to multiple project projects to Gopath, such as setting Gopath as the following path:
d:\workspace\golang\calcproj;d:\workspace\golang\golearning;d:\workspace\golang\rpcclient;d:\workspace\golang\ Rpcserver
Advantages : The project directory structure is clear and clean, each project only has its own relevant documents.
cons : When using a go get command, only the first path under the Gopath path is affected, such as when using go get to obtain the third party source code, the obtained source can only be downloaded to the first path under the Gopath path.
• Programme II
The solution is to always set a gopath path, multiple projects are placed in the SRC directory under the path, and according to the project to divide the directory, for example, set Gopath as the shape of the following path:
D:\workspace\golang
Advantages : Gopath only need to set up a directory to manage multiple projects, also do not worry about third-party source storage location.
disadvantage : This directory structure under the project folder is also a package name path, that is, the internal engineering files in the import need to add the project folder name. In the example above, Protoc-gen-go will need to add a relative path based on SRC when you import other packages: