Download
wget -c https://golang.org/dl/go1.16.4.linux-amd64.tar.gz -O - | sudo tar -xz -C /usr/local
Or
sudo tar -C /usr/local/ -xzf go1.16.4.linux-amd64.tar.gz
Configuration
Add the go_path variable to the configuration file
sudo tee /etc/profile.d/go.sh <<-'EOF'
export GOROOT=/usr/local/go
export GOPATH=~/go
export PATH=$PATH:$GOROOT/bin:$GOPATH
export GO111MODULE=on
export GOPROXY=https://goproxy.cn,direct
EOF
According to the official golang documentation, after go language 1.8, the setting of the GOPATH variable is optional, so it is not necessary to set it here. In this way, the content of the above-mentioned environment variable configuration file can be configured as follows.
sudo tee /etc/profile.d/go.sh <<-'EOF'
export GOROOT=/usr/local/go
#export GOPATH=~/go
#export PATH=$PATH:$GOROOT/bin:$GOPATH
export PATH=$PATH:$GOROOT/bin
export GO111MODULE=on
export GOPROXY=https://goproxy.cn,direct
EOF
Enable the ability to automatically append GOPATH at boot
tee -a ~/.bashrc <<-'EOF'
if [ -f /etc/profile.d/go.sh ]; then
. /etc/profile.d/go.sh
fi
EOF
Make environment variables take effect
source .bashrc
Verification
Verification results
[email protected]:~$ echo $GOPATH
/home/lwk/Public/project/default
[email protected]:~$ echo $GOROOT
/usr/local/go
[email protected]:~$ go version
go version go1.16.4 linux/amd64
[email protected]:~$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/lwk/.cache/go-build"
GOENV="/home/lwk/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/lwk/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/lwk/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.4"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2518064323=/tmp/go-build -gno-record-gcc-switches"
[email protected]:~$