-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgolang.sh
66 lines (53 loc) · 1.67 KB
/
golang.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
# Script Name: golang.sh
# Author: MoeByte
# 判断IP地址是否为中国
if curl -m 10 -s https://ipapi.co/json | grep -q 'China'; then
# 中国用户使用 golang.google.cn/dl/
base_url='https://golang.google.cn/dl/'
go_proxy='https://goproxy.cn'
else
# 非中国用户使用 go.dev/dl/
base_url='https://go.dev/dl/'
go_proxy=''
fi
# 获取所有的Stable版本链接
stable_versions=$(curl -s "$base_url?mode=json" | grep -E 'version|stable' | awk -F '"' '{print $4}' | tr '\n' ' ')
# 选择最新的版本
latest_version=$(echo $stable_versions | tr ' ' '\n' | grep -E 'go[0-9]+\.[0-9]+\.[0-9]+' | sort -rV | head -n 1)
# 检查最新版本是否已经安装
if go version | grep -q $latest_version; then
echo "已经安装了最新的 Go 版本"
exit 0
fi
# 下载最新版本的安装包
download_url="$base_url$latest_version.linux-amd64.tar.gz"
if ! wget -P /tmp $download_url; then
echo "下载失败"
exit 1
fi
# 检查下载的文件是否存在
if [ ! -f "/tmp/$latest_version.linux-amd64.tar.gz" ]; then
echo "下载的文件不存在"
exit 1
fi
# 解压安装包
sudo tar -C /usr/local -xzf /tmp/$latest_version.linux-amd64.tar.gz
# 更新环境变量
if ! grep -q '/usr/local/go/bin' ~/.bashrc; then
echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.bashrc && source ~/.bashrc
fi
# 检查是否成功设置了环境变量
if ! command -v go &> /dev/null; then
echo "环境变量设置失败"
exit 1
fi
# 测试安装
go version
# 如果是中国用户,设置环境变量
if [ "$go_proxy" != "" ]; then
go env -w GO111MODULE=on
go env -w GOPROXY="$go_proxy,direct"
fi
# 删除下载的压缩包
rm /tmp/$latest_version.linux-amd64.tar.gz