go依赖镜像换国内

This commit is contained in:
2025-12-03 17:42:24 +08:00
parent eba59d1429
commit d647807ee6
2 changed files with 106 additions and 11 deletions

38
run.sh
View File

@@ -68,10 +68,44 @@ update_and_build() {
exit 1
fi
# 配置 Go 代理(使用国内镜像)
if [ -z "$GOPROXY" ]; then
# 测试并选择最快的 Go 代理
GO_PROXY_MIRRORS=(
"https://goproxy.cn,direct"
"https://goproxy.io,direct"
"https://mirrors.aliyun.com/go-proxy/,direct"
)
for proxy in "${GO_PROXY_MIRRORS[@]}"; do
proxy_url=$(echo "$proxy" | cut -d',' -f1)
if curl -sf --connect-timeout 2 "${proxy_url}" > /dev/null 2>&1; then
export GOPROXY="$proxy"
export GOSUMDB=sum.golang.google.cn
break
fi
done
# 如果所有镜像都不可用,使用默认配置
if [ -z "$GOPROXY" ]; then
export GOPROXY="https://proxy.golang.org,direct"
export GOSUMDB=sum.golang.google.cn
fi
fi
# 更新依赖
echo -e "${BLUE}更新 Go 依赖...${NC}"
echo -e "${BLUE}更新 Go 依赖(使用代理: ${GOPROXY}...${NC}"
if ! go mod download 2>&1; then
echo -e "${YELLOW}警告: 依赖更新失败,尝试继续编译${NC}"
echo -e "${YELLOW}警告: 依赖更新失败,尝试使用官方源...${NC}"
# 如果失败,尝试使用官方源
OLD_GOPROXY="$GOPROXY"
export GOPROXY="https://proxy.golang.org,direct"
if ! go mod download 2>&1; then
export GOPROXY="$OLD_GOPROXY"
echo -e "${YELLOW}警告: 依赖更新失败,尝试继续编译${NC}"
else
echo -e "${GREEN}✓ 依赖更新完成${NC}"
fi
else
echo -e "${GREEN}✓ 依赖更新完成${NC}"
fi