的版本和下载地址
GO_VERSION="1.22.0"
DOWNLOAD_URL="https://go.dev/dl/go${GO_VERSION}.linux-amd64.tar.gz"
# 检查是否已安装 curl 或 wget
if ! command -v curl &> /dev/null && ! command -v wget &> /dev/null; then
echo "Please install curl or wget first."
exit 1
fi
# 创建临时目录并下载 Go 安装包
TEMP_DIR=$(mktemp -d)
cd "$TEMP_DIR" || exit
echo "Downloading Go ${GO_VERSION}..."
if command -v curl &> /dev/null; then
curl -OL "${DOWNLOAD_URL}"
else
wget "${DOWNLOAD_URL}"
fi
# 检查下载是否成功
if [ $? -ne 0 ]; then
echo "Failed to download Go binary. Please check your internet connection and try again."
rm -rf "$TEMP_DIR"
exit 1
fi
# 解压安装包到 /usr/local 目录下
sudo tar -C /usr/local -xzf "go${GO_VERSION}.linux-amd64.tar.gz"
# 配置环境变量
cat << EOF | sudo tee -a /etc/profile.d/go.sh
export GOPATH=$HOME/go
export GOBIN=\$GOPATH/bin
export PATH=\$PATH:/usr/local/go/bin:\$GOBIN
EOF
# 应用新的环境变量配置
source /etc/profile.d/go.sh
# 清理临时文件
cd .. || exit
rm -rf "$TEMP_DIR"
# 验证安装
go version
echo "Go ${GO_VERSION} has been installed successfully!"
centos7一键安装自动go1.22 脚本
最新推荐文章于 2024-08-19 17:21:58 发布