Files
CmiiDeploy/998-常用脚本/研发环境相关DEMO/安装golang.sh
zeaslity ce4165e36b update
2025-05-15 10:32:14 +08:00

175 lines
4.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# 脚本描述安装Golang并设置国内代理源
# 适用环境Linux系统支持Ubuntu、CentOS等主流发行版
# Go版本1.24.2
# 下载源:阿里云镜像
# 变量定义
GO_VERSION="go1.24.2"
GO_TAR="${GO_VERSION}.linux-amd64.tar.gz"
DOWNLOAD_URL="https://mirrors.aliyun.com/golang/${GO_TAR}"
INSTALL_DIR="/usr/local"
GO_DIR="${INSTALL_DIR}/go"
TMP_DIR="/tmp"
GOPROXY_URL="https://goproxy.cn,direct"
# 颜色输出函数
print_info() {
echo -e "\033[32m[INFO]\033[0m $1"
}
print_error() {
echo -e "\033[31m[ERROR]\033[0m $1"
}
print_warning() {
echo -e "\033[33m[WARNING]\033[0m $1"
}
# 检查是否为root用户或有sudo权限
check_privileges() {
if [ "$EUID" -ne 0 ]; then
if ! command -v sudo &> /dev/null; then
print_error "需要root权限或sudo命令来执行安装请以root用户运行或安装sudo。"
exit 1
fi
print_warning "当前非root用户将使用sudo执行部分命令。"
fi
}
# 检查依赖工具
check_dependencies() {
local deps=("wget" "tar")
for dep in "${deps[@]}"; do
if ! command -v "$dep" &> /dev/null; then
print_info "安装依赖工具 $dep..."
if [ "$EUID" -eq 0 ]; then
apt update && apt install -y "$dep" || yum install -y "$dep" || dnf install -y "$dep"
else
sudo apt update && sudo apt install -y "$dep" || sudo yum install -y "$dep" || sudo dnf install -y "$dep"
fi
if [ $? -ne 0 ]; then
print_error "安装 $dep 失败,请手动安装后再运行脚本。"
exit 1
fi
fi
done
}
# 下载Go安装包
download_go() {
print_info "开始下载Golang安装包..."
if [ -f "${TMP_DIR}/${GO_TAR}" ]; then
print_info "安装包已存在,跳过下载。"
return 0
fi
if ! wget -O "${TMP_DIR}/${GO_TAR}" "$DOWNLOAD_URL"; then
print_error "下载Golang失败请检查网络连接或URL是否有效。"
exit 1
fi
print_info "下载完成:${TMP_DIR}/${GO_TAR}"
}
# 安装Go
install_go() {
print_info "开始安装Golang到${INSTALL_DIR}..."
if [ -d "$GO_DIR" ]; then
print_warning "目录${GO_DIR}已存在,将被覆盖。"
if [ "$EUID" -eq 0 ]; then
rm -rf "$GO_DIR"
else
sudo rm -rf "$GO_DIR"
fi
fi
if [ "$EUID" -eq 0 ]; then
tar -C "$INSTALL_DIR" -xzf "${TMP_DIR}/${GO_TAR}"
else
sudo tar -C "$INSTALL_DIR" -xzf "${TMP_DIR}/${GO_TAR}"
fi
if [ $? -ne 0 ]; then
print_error "解压安装包失败,请检查文件完整性。"
exit 1
fi
print_info "Golang安装完成。"
}
# 设置环境变量
setup_env() {
print_info "设置环境变量..."
local profile_file=""
if [ -f "$HOME/.bashrc" ]; then
profile_file="$HOME/.bashrc"
elif [ -f "$HOME/.bash_profile" ]; then
profile_file="$HOME/.bash_profile"
elif [ -f "$HOME/.zshrc" ]; then
profile_file="$HOME/.zshrc"
else
print_warning "未找到常见的shell配置文件将使用/etc/profile需要root权限。"
profile_file="/etc/profile"
fi
# 检查是否已设置环境变量,避免重复添加
if ! grep -q "/usr/local/go/bin" "$profile_file"; then
cat >> "$profile_file" << 'EOF'
# Go environment variables
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
EOF
print_info "环境变量已写入${profile_file}"
else
print_info "环境变量已存在,跳过写入。"
fi
# 立即生效(仅对当前会话)
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
}
# 设置Go代理
setup_proxy() {
print_info "设置Go国内代理源..."
if command -v go &> /dev/null; then
go env -w GOPROXY="$GOPROXY_URL"
if [ $? -eq 0 ]; then
print_info "代理源已设置为:$GOPROXY_URL"
else
print_error "设置代理源失败,请手动运行 'go env -w GOPROXY=$GOPROXY_URL'"
fi
else
print_error "Go命令未找到代理设置失败。请检查安装是否成功。"
fi
}
# 验证安装
verify_installation() {
print_info "验证Golang安装..."
if command -v go &> /dev/null; then
go_version=$(go version)
print_info "Golang版本$go_version"
goproxy=$(go env GOPROXY)
print_info "GOPROXY设置$goproxy"
else
print_error "Golang未正确安装请检查错误信息。"
exit 1
fi
}
# 主函数
main() {
check_privileges
check_dependencies
download_go
install_go
setup_env
setup_proxy
verify_installation
print_info "Golang安装和配置完成请运行 'source ~/.bashrc' 或重新登录以应用环境变量。"
}
# 执行主函数
main