banner
SlhwSR

SlhwSR

热爱技术的一名全栈开发者
github
bilibili

Environment variables

2.2 Go Environment Variables#

The Go development environment relies on some operating system environment variables, and it is best to have them set up before installing Go. If you are using Windows, you do not need to manually set them up as Go will be installed by default in the directory c:/go. Here are a few of the most important environment variables:

  • $GOROOT represents the installation location of Go on your computer, and its value is usually $HOME/go, but you can also install it elsewhere.
  • $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 represents the installation location of the compiler and linker, and its default value is $GOROOT/bin. If you are using Go 1.0.3 or later versions, you can generally leave its value empty, and Go will use the default value mentioned earlier.

The target machine refers to the machine on which you intend to run your Go application.

The Go compiler supports cross-compilation, which means you can build applications that can run on different operating systems and processor architectures on a single machine. This means that the machine on which you write the source code can have completely different characteristics (operating system and processor architecture) from the target machine.

To distinguish between the local machine and the target machine, you can use $GOHOSTOS and $GOHOSTARCH to set the operating system name and compilation architecture of the local machine. These two variables are only used when cross-compiling, and if you do not explicitly set them, their values will be the same as the local machine ($GOOS and $GOARCH).

  • $GOPATH defaults to the same value as $GOROOT, but starting from Go version 1.1, you must modify it to a different path. It can include multiple paths for Go language source code files, package files, and executable files, and these paths must each contain three specified directories: src, pkg, and bin. These three directories are used to store source code files, package files, and executable files, respectively.
  • $GOARM is specifically for processors based on the arm architecture, and its value can be 5 or 6, with 6 being the default.
  • $GOMAXPROCS is used to set the number of processors and cores that the application can use.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.