为什么我在 GoLand 中看不到某些环境变量,而我可以从终端看到它们?

问题描述 投票:0回答:1

我在 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".

它们为什么不同?

go variables terminal environment envvar
1个回答
0
投票

环境的价值。变种

NO_PROXY
你可以在终端看到来自
.bash_profile
:

export NO_PROXY="*.googleapis.com"

如果程序是从终端执行的,它可以看到这个值。

但是,如果从 Dock 启动 GoLand,则不会执行

.bash_profile
,因此不会设置该变量。

一些解决方案:

  1. 从终端启动 Goland,而不是 Dock:
    /Applications/GoLand.app/Contents/MacOS/goland
    。这样,它将看到
    .bash_profile
    指定的所有变量。
  2. 将环境变量添加到 GoLand 中的运行配置:
    Run
    Edit configurations
    Environment
    NO_PROXY=*.googleapis.com;no_proxy=*.googleapis.com
    。这样,只有手动添加的变量才可用于代码。

注意:如果您想更轻松地访问,可以通过

Tools
Create Command-line Launcher
创建永久命令行启动器。

© www.soinside.com 2019 - 2024. All rights reserved.