我在 MacOS 中有以下 go 程序:
package main
import (
"fmt"
"os"
)
func main() {
name := "NO_PROXY"
fmt.Printf("value of the env. var. \"%s\" is \"%s\".\n", name, os.Getenv(name))
}
如果我在 IDE 中运行它(运行或调试),它显示变量为空:
value of the env. var. "NO_PROXY" is "".
相比之下,如果我通过
go run main.go
从终端运行相同的脚本,那么它会显示
value of the env. var. "NO_PROXY" is "*.googleapis.com".
它们为什么不同?
环境的价值。变种
NO_PROXY
你可以在终端看到来自.bash_profile
:
export NO_PROXY="*.googleapis.com"
如果程序是从终端执行的,它可以看到这个值。
但是,如果从 Dock 启动 GoLand,则不会执行
.bash_profile
,因此不会设置该变量。
一些解决方案:
/Applications/GoLand.app/Contents/MacOS/goland
。这样,它将看到 .bash_profile
指定的所有变量。Run
→ Edit configurations
→ Environment
→ NO_PROXY=*.googleapis.com;no_proxy=*.googleapis.com
。这样,只有手动添加的变量才可用于代码。注意:如果您想更轻松地访问,可以通过
Tools
→ Create Command-line Launcher
创建永久命令行启动器。