This commit is contained in:
qing committed 2026-07-25 01:38:42 +08:00
1 parent 76e3a06c97
commit b66c267268
2 files changed
+25 -10

No files matched your search

+20 -10
View File
@@ -1,28 +1,38 @@
#!/bin/bash
# 设置 Go 模块代理为国内镜像,加速依赖下载
# 仅当用户未显式设置 GOPROXY 或仍为默认值时修改
# 设计要点:
# 1. 既 export 到当前 shell(保证后续 go 命令立即可用)
# 2. 也 go env -w 持久化(保证后续会话可用)
# 3. 用户已自配非默认代理时尊重配置不覆盖
# 用法:source _goproxy.sh (由 _set.sh 调用,需在同 shell 生效)
set -e
if ! command -v go >/dev/null 2>&1; then
echo "[skip] 未找到 go,跳过 GOPROXY 设置"
exit 0
return 0 2>/dev/null || exit 0
fi
CURRENT=$(go env GOPROXY)
DEFAULT_PROXY="https://proxy.golang.org,direct"
# 已显式设置为非默认值(如用户自配 corporate proxy),尊重用户配置不覆盖
if [ "$CURRENT" != "https://proxy.golang.org,direct" ] && [ -n "$CURRENT" ]; then
# 用户已显式设置为非默认值(corporate proxy 等),尊重不覆盖,但仍 export 保证当前 shell 一致
if [ "$CURRENT" != "$DEFAULT_PROXY" ] && [ -n "$CURRENT" ]; then
echo "[skip] GOPROXY 已配置为: $CURRENT"
exit 0
export GOPROXY="$CURRENT"
return 0 2>/dev/null || exit 0
fi
# 优先用环境变量 GOPROXY(临时设置),否则写入 go env 持久化
if [ -n "${GOPROXY:-}" ]; then
# 用户已用环境变量临时设置,直接用
if [ -n "${GOPROXY:-}" ] && [ "${GOPROXY:-}" != "$DEFAULT_PROXY" ]; then
echo "[ok] 使用环境变量 GOPROXY=$GOPROXY"
exit 0
return 0 2>/dev/null || exit 0
fi
echo "[setup] 永久设置 GOPROXY=https://goproxy.cn,direct"
# 切到国内镜像
echo "[setup] 设置 GOPROXY=https://goproxy.cn,direct"
go env -w GOPROXY=https://goproxy.cn,direct
go env -w GOSUMDB=sum.golang.google.cn
echo "[ok] 当前 GOPROXY=$(go env GOPROXY) GOSUMDB=$(go env GOSUMDB)"
# 同时 export 到当前 shell,确保本会话内 go 命令立即生效
export GOPROXY=https://goproxy.cn,direct
export GOSUMDB=sum.golang.google.cn
echo "[ok] GOPROXY=$GOPROXY GOSUMDB=$GOSUMDB"