No matter you are using windows, Linux or Mac OS operating system to develop go applications, after installing the go installation language development tools, you must configure the environment variables required by the go language development to complete the construction of the go development environment.
However, for some beginners, they may not understand the role of environment variables commonly used in go language and how to set environment variables. Let’s talk about it today.
Note: after the installation of the windows operating system installation package (. MSI suffix) provided by go is completed, several commonly used environment variables will be automatically configured.
Common environment variables
There are many environment variables that can be set in go language. Each environment variable has its own function, but many of them may not be used by us. Generally, we need to understand and the most important ones are as follows:
$go env // print all default go environment variables $go env gopath // print the value of an environment variable
The environment variable goroot represents the installation directory of the go language.
In windows, the default value of goroot is C / go, while in Mac OS or Linux, the default value of goroot is usr / loca / go. If go is installed in other directories, the value of goroot needs to be changed to the corresponding directory.
In addition, goroot / bin contains the tool chain provided by go. Therefore, we should configure goroot / bin into the environment variable path to facilitate us to use the go tool chain globally.
Linux setup goroot demo
export GOROOT=~/go export PATH=$PATH:$GOROOT/bin
Note that gopath cannot have the same value as goroot.
The environment variable gopath is used to specify our development workspace, which is used to store source code, test files, library static files and executable files.
In UNIX like (MAC OS or Linux) operating systems, the default value of gopath is $home / go. In windows, the default value of gopath is% userprofile% \ \ go (for example, in the admin user, the value is C: \.
Of course, we can change the workspace by modifying gopath. For example, we can set the opt / go mode as follows:
Linux setup gopath demo
In addition, you can set up multiple workspaces in gopath, such as:
Subdirectories of gopath
The code above indicates that we specify two workspaces. However, when we use the go get command to get the remote library, it is usually installed in the first workspace.
According to the go development specification, each work in gopath directory is generally divided into three subdirectories: SRC, PKG, and bin. Therefore, each workspace we see looks like this:
Hello ා executable
hello.go Command line code
main.go Command line code
main_ test.go Test code
reverse.go Database file
reverse_ test.go Database file
reader.go Database file
writer.go Database file
SRC directory is the source code file we developed, the corresponding directory below is called package, PKG is the compiled library static file, bin is the source code compilation background executable file.
The environment variable Gobin represents the installation directory of binary commands after compiling our development program.
When we use the go install command to compile and package applications, the command will package the compiled binary program into the Gobin directory. Generally, we set Gobin to gopath / bin directory.
Linux setting Gobin demo
In the above code, we use the export command to set the environment variables, so that the settings can only be valid in the current shell. If you want to keep the environment variables valid, such as in Linux, you should add the environment variables to the files such as / etc / profile.
What is cross compilation? The so-called cross compilation refers to the generation of code that can run on another platform on one platform. For example, we can generate binary programs that can run on 64 bit Linux operating system on 32-bit windows operating system development environment.
Cross compilation in other programming languages may require the help of third-party tools, but cross compilation in go language is very simple. The simplest way is to set the environment variables goos and goarch.
Goos and goarch
The default value of goos is our current operating system. For windows and Linux, note that the value of Mac OS operation is Darwin. Goarch means CPU architecture, such as 386, AMD64, arm, etc.
Get the values of goos and goarch
We can use the go env command to get the current values of goos and goarch.
$ go env GOOS GOARCH
Range of goos and goarch
The values of goos and goarch appear in pairs and can only be the values corresponding to the following list.
Compiling target programs running on 64 bit Linux operating system
$ GOOS=linux GOARCH=amd64 go build main.go
Compiling the target program of Android operation based on ARM architecture
$ GOOS=android GOARCH=arm GOARM=7 go build main.go
List of environment variables
Although we usually configure only a few environment variables, in fact, go language provides a lot of environment variables, so that we can freely customize the development and compiler behavior.
The following is a list of all the environment variables provided by go. Generally, it can be divided into the following categories. You can have a general understanding of them, because some environment variables can never be used by us.
Through environment variables
Environment variables used with CGO
CGO_CPPFLAGS, CGO_CPPFLAGS_ALLOW, CGO_CPPFLAGS_DISALLOW
CGO_CXXFLAGS, CGO_CXXFLAGS_ALLOW, CGO_CXXFLAGS_DISALLOW
CGO_FFLAGS, CGO_FFLAGS_ALLOW, CGO_FFLAGS_DISALLOW
CGO_LDFLAGS, CGO_LDFLAGS_ALLOW, CGO_LDFLAGS_DISALLOW
Environment variables related to system architecture
Special environment variables
Other environment variables
The setting of environment variables can affect the process and results of our development and compilation projects, so it is necessary to understand.
For more information about the description and settings of common environment variables in golang, please see the related links below