Getting started with Go language-environment setup (2)

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:
Insert picture description here

  • 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.
  • GOPATHIt'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/Workspacebelow, but you can modify it according to your own needs.
  • GOBINGenerally 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 the go installcommand in your own project, the compiled executable file will be placed here by default.
  • PATH=$PATH:$GOROOT:$GOBINThis is to add the executable file directory GOROOTand GOBINappend 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 / , It GOPROXYcan be pulled normally under configuration .
  • GOPRIVATE
    Some classmates’ projects must reference their company’s gitlab private warehouse, so they also need to be equipped, GOPRIVATEwhich means that the GOPROXYconfigured 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 GOENVviewed through commands .

    go env -w key=valueThe file can be modified directly through commandsInsert picture description here

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 geterror 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 ~/.gitconfigfile, 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/

Guess you like

Origin blog.csdn.net/weixin_52777294/article/details/113201535