Whether you are using Windows, Linux or Mac OS operating systems to develop Go applications, after installing Go language development tools, you must configure the environment variables required for Go language development to be considered as initially completing the construction of the Go development environment.
But for some beginners, they may not understand the role of environment variables commonly used in Go language and how to set environment variables. Today we will talk about it.
Note: After the 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, and each environment variable has its own function, but we may not use many of them. Generally, the most important ones that need to be understood are the following:
$ go env //Print all default environment variables of Go$ go env GOPATH //Print the value of an environment variable
GOROOT
The environment variable GOROOT represents the installation directory of 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 another directory, the value of GOROOT needs to be modified to the corresponding directory.
In addition, GOROOT/bin contains the toolchain provided by Go. Therefore, GOROOT/bin should be configured into the environment variable PATH to facilitate us to use the Go toolchain globally.
Linux Settings GOROOT Demo
export GOROOT=~/go export PATH=$PATH:$GOROOT/bin
GOPATH
Note that the value of GOPATH cannot be the same as GOROOT.
The environment variable GOPATH is used to specify our development workspace, which is a work that stores source code, test files, library static files, and executable files.
The default value of GOPATH in Unix-like (Mac OS or Linux) operating systems is $home/go. In Windows, the default value of GOPATH is %USERPROFILE%\go (for example, for Admin users, its value is C:\Users\Admin\go).
Of course, we can change the workspace by modifying GOPATH, for example, setting the work opt/go method as follows:
Linux Settings GOPATH Demo
export GOPATH=/opt/go
Also, multiple workspaces can be set in GOPATH, such as:
export GOPATH=/opt/go;$home/go
Subdirectory of GOPATH
The above code means that we specify two workspaces, but when we use the go get command to obtain the remote library, it will usually be installed in the first workspace.
According to the Go development specification, each work in the GOPATH directory is generally divided into three subdirectories: src, pkg, bin, so each workspace we see is like this:
bin/
hello # Executable
outyet # Executable
src/
/golang/example/
.git/
hello/
#Command line code
outyet/
#Command line code
main_test.go # Test code
stringutil/
# Library File
reverse_test.go # Library file
/x/image/
.git/
bmp/
# Library File
# Library File
The src directory holds the source code file we developed, and the corresponding directory below is called package, pkg holds the compiled library static file, and bin holds the executable file in the background of the source code compilation.
GOBIN
The environment variable GOBIN represents the installation directory of binary commands after the development program is compiled.
When we use the go install command to compile and package the application, the command will package the compiled binary program into the GOBIN directory. Generally, we set GOBIN to the GOPATH/bin directory.
Linux Settings GOBIN Demo
export GOBIN=$GOPATH/bin
In the above code, we use the export command to set environment variables, so that the settings can only be valid in the current shell. If you want to be valid all the time, such as in Linux, you should add environment variables to files such as /etc/profile.
Cross-compilation
What is cross-compilation? The so-called cross-compilation refers to the generation of code that can be run on another platform on one platform. For example, we can generate binary programs that can be run on a 64-bit Linux operating system in a 32-bit Windows operating system development environment.
Cross-compilation in other programming languages may require third-party tools, but cross-compilation in Go is very simple. The easiest thing is to set two environment variables, GOOS and GOARCH.
GOOS and GOARCH
The default value of GOOS is our current operating system. If you want to use Windows, Linux, please note that the value on the Mac Os operation is darwin. GOARCH represents 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 GOOS and GOARCH values.
$ go env GOOS GOARCH
Value ranges for GOOS and GOARCH
The values of GOOS and GOARCH appear in pairs, and can only be the values corresponding to the list below.
$GOOS $GOARCH
android arm
darwin 386
darwin amd64
darwin arm
darwin arm64
dragonfly amd64
freebsd 386
freebsd amd64
freebsd arm
linux 386
linux amd64
linux arm
linux arm64
linux ppc64
linux ppc64le
linux mips
linux mipsle
linux mips64
linux mips64le
linux s390x
netbsd 386
netbsd amd64
netbsd arm
openbsd 386
openbsd amd64
openbsd arm
plan9 386
plan9 amd64
solaris amd64
windows 386
windows amd64
Example
Compile target programs running on 64-bit Linux operating systems
$ GOOS=linux GOARCH=amd64 go build
Compile the target program on Android operation of the arm architecture
$ GOOS=android GOARCH=arm GOARM=7 go build
Environment variable list
Although we generally configure only a few environment variables, Go actually provides a lot of environment variables, allowing us to freely customize development and compiler behavior.
Below is a list of all the environment variables provided by Go. They can generally be divided into the following categories. Just a brief look at them, because we will never use some environment variables.
Through environment variables
GCCGO
GOARCH
GOBIN
GOCACHE
GOFLAGS
GOOS
GOPATH
GOPROXY
GORACE
GOROOT
GOTMPDIR
Environment variables used with cgo
CC
CGO_ENABLED
CGO_CFLAGS
CGO_CFLAGS_ALLOW
CGO_CFLAGS_DISALLOW
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
CXX
PKG_CONFIG
AR
Environment variables related to system architecture
GOARM
GO386
GOMIPS
GOMIPS64
Dedicated environment variables
GCCGOTOOLDIR
GOROOT_FINAL
GO_EXTLINK_ENABLED
GIT_ALLOW_PROTOCOL
Other environment variables
GOEXE
GOHOSTARCH
GOHOSTOS
GOMOD
GOTOOLDIR
summary
The setting of environment variables can affect the process and results of our development and compilation projects, so it is still necessary to understand them.
For more information and settings of Golang's commonly used environment variables, please see the relevant links below