The original author, the public number [programmers reading], welcome to pay attention to the public number, reprint articles please indicate the source oh.
Whether you are using Windows,Linux or Mac OS operating system to develop Go applications, after installing the Go installation language development tools, must configure the environment variables required by the Go language development, the preliminary completion of the Go development environment.
But for some beginners, may not understand the Go language commonly used Environment variables and how to set Environment variables, today we will talk about.
Note: After the Windows OS installation package (.msi) provided by Go is installed, several commonly used environment variables are automatically configured.
Common environment variables
There are many environment variables that can be set in the Go language. Each environment variable has a role, but many of them may not be used. The most important ones to understand are:
$go env // Prints all default go environment variables. $go env GOPATH // Prints the value of an environment variableCopy the code
GOROOT
The environment variable GOROOT indicates the Go installation directory.
In Windows, the default value for GOROOT is C:/go. In Mac OS or Linux, the default value for GOROOT is usr/loca/go. If go is installed in another directory, change the GOROOT value to the corresponding directory.
In addition, GOROOT/bin contains the toolchain provided by Go. Therefore, GOROOT/bin should be configured in the environment variable PATH to facilitate the use of the Go toolchain globally.
GOROOT demo for Linux setup
export GOROOT=~/go
export PATH=$PATH:$GOROOT/bin
Copy the code
GOPATH
Note that GOPATH cannot have the same value as GOROOT.
The environment variable GOPATH is used to specify our development workspace (workspace), which is the work of storing source code, test files, library static files, and executable files.
The default value for GOPATH on UNIX-like (Mac OS or Linux) operating systems is $HOME /go. In Windows, the default GOPATH value is %USERPROFILE%\go(for example, C:\Users\Admin\go).
Of course, we can change the workspace by modifying GOPATH. For example, set the work to opt/ GO as follows:
Linux setup GOPATH demo
export GOPATH=/opt/go
Copy the code
Also, you can set up multiple workspaces in GOPATH, such as:
export GOPATH=/opt/go;$home/go
Copy the code
Subdirectory of GOPATH
The above code indicates that we specify two workspaces, but when we use the go get command to obtain the remote library, it is usually 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 we see each workspace looks like this:
bin/
hello # Executable file
outyet # Executable file
src/
github.com/golang/example/
.git/
hello/
hello.go # Command line code
outyet/
main.go # Command line code
main_test.go # Test code
stringutil/
reverse.go # libraries
reverse_test.go # libraries
golang.org/x/image/
.git/
bmp/
reader.go # libraries
writer.go # libraries
Copy the code
The SRC directory contains the source code files we developed, and the corresponding directories below are called packages, PKG contains the compiled library static files, and bin contains the executable files behind the source code compilation.
GOBIN
The environment variable GOBIN represents the installation directory of the binary commands that our development program compiled.
When we compile and package an application using the go install command, the command packages the compiled binaries into the GOBIN directory, which we normally set to the GOPATH/bin directory.
Linux setup GOBIN demo
export GOBIN=$GOPATH/bin
Copy the code
In the above code, we set the environment variables using the export command, so that the Settings are valid only in the current shell. If you want to keep the Settings valid, as in Linux, you should add the environment variables to a file such as /etc/profile.
cross-compilation
What is cross-compilation? By cross-compiling, we mean generating code on one platform that runs on another. For example, we can generate binary programs on 32-bit Windows operating system development environment that run on 64-bit Linux operating system.
Cross-compiling in other programming languages might require third-party tools, but cross-compiling in Go is as simple as setting the GOOS and GOARCH environment variables.
GOOS and GOARCH
The default value of GOOS is our current operating system, if Windows, Linux, notice that the value on the MAC OS operation is Darwin. GOARCH talked about CPU architectures such as 386, AMD64, ARM, etc.
Gets the values for GOOS and GOARCH
We can use the go env command to get the current GOOS and GOARCH values.
$ go env GOOS GOARCH
Copy the code
The value range of GOOS and GOARCH
The values of GOOS and GOARCH appear in pairs and can only be those corresponding to the list below.
$GOOS $GOARCHandroid 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 amd64Copy the code
The sample
Builds an object program to run on a 64-bit Linux operating system
$ GOOS=linux GOARCH=amd64 go build main.go
Copy the code
Compile the target program on the ARM architecture Android operation
$ GOOS=android GOARCH=arm GOARM=7 go build main.go
Copy the code
List of environment variables
Although we usually only configure a few environment variables, the Go language provides a large number of environment variables that allow us to customize development and compiler behavior freely.
The following is a list of all the environment variables that Go provides, which can be broken down into the following categories, just to be familiar with some of the environment variables that you may never use.
Through the environment variable
GCCGO
GOARCH
GOBIN
GOCACHE
GOFLAGS
GOOS
GOPATH
GOPROXY
GORACE
GOROOT
GOTMPDIR
Copy the code
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
Copy the code
Environment variables related to the system architecture architecture
GOARM
GO386
GOMIPS
GOMIPS64
Copy the code
Dedicated environment variables
GCCGOTOOLDIR
GOROOT_FINAL
GO_EXTLINK_ENABLED
GIT_ALLOW_PROTOCOL
Copy the code
Other environment variables
GOEXE
GOHOSTARCH
GOHOSTOS
GOMOD
GOTOOLDIR
Copy the code
summary
The setting of environment variables can affect the process and results of developing and compiling a project, so it is worth knowing.
Your attention is my greatest encouragement on the way of writing!