Go Environment variables
The Go development environment is dependent on some operating system environment variables, so you'd better set them up when you install go. If you are using Windows, you do not have to manually set up, Go will be installed by default in the directory c:/go
. Here are some of the most important environment variables:
- $GOROOT indicates where Go is installed on your PC, and it's usually the
$HOME/go
same value, and you can install it somewhere else, of course.
- $GOARCH represents the processor architecture of the target machine, and its value can be 386, AMD64, or arm.
- $GOOS represents the operating system of the target machine, and its value can be Darwin, FreeBSD, Linux, or Windows.
- $GOBIN indicates where the compiler and linker are installed, by default
$GOROOT/bin
, if you are using Go 1.0.3 and later versions, you can normally set its value to NULL, and Go will use the default values mentioned earlier.
The target machine is the machine on which you intend to run your Go application.
The Go compiler supports cross-compiling, meaning that you can build applications running on a single machine that run on different operating systems and processor architectures, meaning that the machine that writes the source code can have a completely different feature (operating system and processor architecture) than the target machine.
In order to distinguish between the local machine and the target machine, you can use $GOHOSTOS
and $GOHOSTARCH
set the parameters of the target machine, these two variables are only used when cross-compiling, if you do not display the settings, their values will be the same as the local machine ( $GOOS
and $GOARCH
).
- $GOPATH defaults to
$GOROOT
the same value, but from the Go 1.1 version you have to change to a different path. It can contain multiple paths containing the Go language source files, package files, and executable files, which must contain three specified directories: src
, pkg
and bin
, respectively, these three directories are used to store source files, package files, and executable files.
- $GOARM specifically for ARM architecture-based processors, the value can be 5 or 6, which defaults to 6.
- $GOMAXPROCS is used to set the number of processors and the number of cores that the application can use, as described in section 14th. 1.3.
In the following chapters, we will discuss how to install the Go language on Linux, Mac OS X, and Windows. The installation on FreeBSD is very similar to Linux. The development team is trying to port the go language to other examples such as OpenBSD, Dragonflybsd, NetBSD, Plan 9, Haiku, and Solaris, where you can find the latest news: Go Porting efforts.
Excerpt from: https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/02.2.md
Setting the GO environment variable
In Linux, we typically $HOME/.bashrc
configure custom environment variables by file, depending on the distribution, may be files $HOME/.profile
, and then use Gedit or VI to edit the contents of the file.
Export goroot= $HOME/go
To ensure that the relevant files can be called anywhere in the file system, you will also need to add the following:
Export path= $PATH: $GOROOT/bin
When you develop a Go project, you also need an environment variable to save your working directory.
Export Gopath= $HOME/applications/go
$GOPATH
Can contain multiple working directories, depending on your personal situation. If you set up more than one working directory, the go get
remote package will be installed in the first directory when you use it later (Remote Package Install command).
After you have completed these settings, you need to enter instructions at the terminal source .bashrc
to make these environment variables effective. Then restart the terminal, enter go env
and env
check to see if the environment variable is set correctly.
Download the Go source package from the official page or the domestic image to your computer, then move the extracted directory go
through the command to the $GOROOT
location you are pointing to.
wget https://storage.googleapis.com/golang/go<VERSION>.src.tar.gz tar-zxvf go<version>.src.tar.gz sudo MV Go $GOROOT
Go language note--go environment variable goroot is the path that is installed and Gopath is a three-party package path