The previous article is relatively simple, here are some environment variables commonly used in Go.
After execution go env
, you can see the environment variables currently used by go:
GOROOT
It is the path where the go command is located. The default value is /usr/local/go/bin, and there are some official packages that come with go under /usr/local/go.GOPATH
It's a historical problem. It will be discarded when go's package management tool is improved, but now it is equipped first, and it is still useful. The cache of go mod will be placed in this path by default. I am generally used to define it$HOME/Workspace
below, but you can modify it according to your own needs.GOBIN
Generally used to store some third-party, go-related executable files. For example, if you use vscode, you need to put the next bunch of things in this directory. Or if you execute thego install
command in your own project, the compiled executable file will be placed here by default.PATH=$PATH:$GOROOT:$GOBIN
This is to add the executable file directoryGOROOT
andGOBIN
append it to the system environment variable$PATH
. After entering the command on the command line, the operating system can search in it.GO111MODULE
It means whether to enable go mod for the project to manage dependencies. This suggestion is set to auto, preferably on, after all, go mod is the trend.GOPROXY
Because many packages of go are on github, and some rely on golang.org, they can’t be downloaded during domestic build, so you need to configure a proxy. Now the most used one is https://goproxy.io/zh / , ItGOPROXY
can be pulled normally under configuration .GOPRIVATE
Some classmates’ projects must reference their company’s gitlab private warehouse, so they also need to be equipped,GOPRIVATE
which means that theGOPROXY
configured agent will not be used when pulling the private warehouse .
The above environment variables can be defined in two places:
- The preloaded file of the shell (usually
~/.bashrc
, for users who use zsh~/.zshrc
) - Go special environment variable configuration file, the specific file location can be
go env GOENV
viewed through commands .go env -w key=value
The file can be modified directly through commands
The following is the configuration added in my own ~/.bashrc
# 比较通用的配置
export GOROOT=/usr/local/go/bin
export GOPATH=$HOME/Workspace
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOROOT:$GOBIN
# 这几个配置放在这或者使用`go env -w k=v`都可以
export GO111MODULE=auto
export GOPROXY="https://goproxy.io,direct"
# export GOPRIVATE=git.hello.com
If you encounter an go get
error when using a private warehouse or you can’t pull the private warehouse normally during go build, you need to do the following configuration,
edit the ~/.gitconfig
file, add the following content, pay attention to replace git.hello.com with the git warehouse address you actually use.
[url "ssh://git.hello.com/"]
insteadOf = https://git.hello.com/