goland 启动时未读取 goprivate 是因默认不继承系统 shell 环境变量,需在 run → edit configurations → environment variables 中手动添加 goprivate=gitlab.example.com,github.company.com 或勾选 “load environment variables from shell”。

GoLand 启动时没读到 GOPRIVATE 环境变量
GoLand 默认不继承系统 shell 的环境变量(比如你在 ~/.zshrc 里写的 export GOPRIVATE=gitlab.example.com),所以即使终端里 go mod tidy 正常,GoLand 里仍会报 “verifying xxx: reading https://sum.golang.org/lookup/...” 或 “repository not found”。
解决方法是让 GoLand 显式加载这些变量:
- 打开 GoLand → Help → Edit Custom VM Options…,添加一行:
-Didea.environment.variables=true - 然后进入 File → Settings → Go → GOPATH(或 Preferences → Go → GOPATH on macOS),勾选 “Load environment variables from shell”
- 更稳妥的做法:在 GoLand 的 Run → Edit Configurations… → Templates → Go Build / Go Test 里,点开 Environment variables,手动添加:
GOPRIVATE=gitlab.example.com,github.company.com - Windows 用户若用
set GOPRIVATE=...命令临时设置,该值只对当前 CMD 生效;必须通过系统属性 → 高级 → 环境变量 → 新建用户/系统变量才持久
Git 凭据没被 GoLand 的 git clone 进程识别
Go 拉私有模块本质是调用 git clone,而 GoLand 内置的 Git 可能用的是它自带的 git 二进制(而非你 CLI 里配置好的那个),导致 SSH 密钥或 ~/.netrc 不生效。
确认并修复方式:
- 在 GoLand 终端里运行
which git和git config --global credential.helper,看是否和你命令行一致 - 进入 Settings → Version Control → Git,检查 Path to Git executable 是否指向你预期的 git(例如
/usr/bin/git或C:\Program Files\Git\bin\git.exe) - 如果用 SSH,确保
ssh -T git@gitlab.example.com在 GoLand 终端里能成功;失败则说明 agent 没加载密钥,需在 GoLand 启动前运行eval $(ssh-agent); ssh-add ~/.ssh/id_rsa - 避免混用 HTTPS 和 SSH:go.mod 中
module路径必须匹配你期望的协议,比如写gitlab.example.com/group/lib,而不是https://gitlab.example.com/group/lib
GO111MODULE=off 导致 GOPRIVATE 完全失效
GoLand 有时会因项目根目录没有 go.mod 或设置了旧版 GOPATH 模式,自动降级为 GO111MODULE=off。此时 GOPRIVATE、GOPROXY 全部被忽略,所有依赖都走 GOPATH,自然拉不到私有模块。
检查与强制启用:
- 在 GoLand 终端执行
go env GO111MODULE,输出必须是on;如果不是,执行go env -w GO111MODULE=on - 确保项目根目录存在
go.mod文件;若没有,先在终端运行go mod init example.com/myapp - GoLand 的 Settings → Go → Modules 里,确认 “Enable Go modules integration” 已勾选
- 重启 GoLand —— 环境变量变更和模块配置改动往往需要重启 IDE 才完全生效
GOPRIVATE 匹配规则写错导致代理仍拦截
GOPRIVATE 是前缀匹配,不是域名匹配。写成 GOPRIVATE=*.gitlab.example.com 或 GOPRIVATE=gitlab.example.com/* 都不对;正确写法是 GOPRIVATE=gitlab.example.com,它会匹配所有以该字符串开头的 module path,比如 gitlab.example.com/group/lib、gitlab.example.com/group/lib/v2。
常见错误:
- 多域名用空格或分号分隔 → 必须用英文逗号:
GOPRIVATE=gitlab.example.com,github.company.com - module path 是
gitlab.internal/team/repo,但 GOPRIVATE 设成了gitlab.example.com→ 不匹配,得设成gitlab.internal - 用了通配符嵌套,如
gitlab.*/group/*→ Go 不支持,只认纯前缀 - 忘记同时设置
GONOSUMDB,导致 sum.golang.org 校验失败中断;建议一并设为相同值:GONOSUMDB=gitlab.example.com
GOPRIVATE 没加载;与其反复调试 shell 加载逻辑,不如直接在 Run Configuration 里硬编码环境变量——简单、稳定、不依赖 IDE 对 shell 的解析能力。











