linux部署go多版本工具g

Table of Contents

安装

cd
mkdir ~/.g
cat << "EOF" | tee ~/.g/env
#!/bin/sh
# g shell setup
export GOROOT="${HOME}/.g/go"
export PATH="${HOME}/.g/bin:${GOROOT}/bin:$PATH"
export G_MIRROR=https://golang.google.cn/dl/
EOF

cat << "EOF" | tee -a ~/.bashrc
# g shell setup
if [ -f "${HOME}/.g/env" ]; then
    . "${HOME}/.g/env"
fi
EOF

exec bash

# download g
wget -c https://mirror.ghproxy.com/https://github.com/voidint/g/releases/download/v1.6.0/g1.6.0.linux-amd64.tar.gz
mkdir ~/.g/bin
# unzip to folder ~/.g/bin
tar xf g1.6.0.linux-amd64.tar.gz -C ~/.g/bin
rm -f g1.6.0.linux-amd64.tar.gz 2>/dev/null
ls ~/.g/bin
exec bash
# list current env
g ls
# list remote go versions
g ls-remote
g ls-remote stable
# install go version
g install 1.20.11
g install 1.21.4
# activate go version
g use 1.20.11
g clean
# update g
g self update
# remove g
g self uninstall

FAQ

安装go版本报错[g] Url "https://golang.google.cn/dl/" is unreachable ==> Get "https://golang.google.cn/dl/": net/http: TLS handshake timeout

需要梯子访问Google才行,而且还要开全局代理。

为go设置国内代理

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
Posted in go