The go language is not mandatory to use a certain workspace and project structure, for a small go program to rely on the go run and other commands can be directly compiled run. However, maintaining a good workspace and file structure is very helpful for managing source code and publishing programs. Working space is a must for large go language projects.
1. Work space structure of Go language
The Go language workspace is actually a file directory that must contain SRC, pkg, bin three directories.
Where the SRC directory is used for the Go source code, the PKG directory is used for the package object, and the bin directory is used to hold executable objects. Use the Go Compiler command tool to store source code or package compiled binary output in bin and pkg catalogs. The source code in the SRC directory is categorized into the corresponding subdirectory according to the package name, and various version control tools are available. For example, the working space directory structure of Go is as follows:
bin/ Hello # executable command outyet # executable command pkg/ linux_amd64/ github.com/golang/example/ STRINGUTIL.A # Package Object src/ github.com/golang/example/ . git/ # git warehouse data hello/ Hello.go # source code outyet/ main.go # source code main_test.go # test source code stringutil/ reverse.go # Package source code reverse_test.go # test source code
The workspace above contains a warehouse called example, which contains the two commands for Hello and Outyet, and a stringutil library. In addition, a workspace typically contains multiple warehouses.
2. Gopath Environment variables
Gopath is a workspace-related environment variable in the go language that specifies the workspace location of the go language.
When you create a workspace directory, you need to add the path of the workspace directory to the GOPATH environment variable. The GOPATH environment variable supports multiple values, and if you have multiple workspaces, you can add multiple workspace values to the environment variable, and the window system uses a semicolon ";" Separating different values, a Linux or Unix system uses a colon ":" to separate different values. Also, add the bin path for all workspaces to the PATH environment variable. Under the Linux system, you can add the following at the end of the ~/.profile file:
$ export Gopath= your workspace path $ export path= $PATH: $GOPATH/bin
Of course, if you have more than one workspace, you cannot use $gopath/bin directly in the path variable, adding the bin path in each workspace separately.
It is important to note that the value of the GOPATH environment variable cannot be the same as the installed Go directory. The Go directory also has a directory structure like SRC, pkg, bin and other similar workspaces, but it contains the standard module of go, it is best not to talk about your workspace and go directory mix, for later upgrade go version is also easier.
Working space and Gopath environment variables for go language