为什么`go env`命令不反映环境变量的更改?

huangapple go评论88阅读模式
英文:

Why doesn't `go env` command reflect change in environment variable?

问题

在我的系统中,GOBIN变量被取消设置(为空值)。现在与其他一些GO环境变量不同,即使在go env命令的输出中,它似乎也是未设置的(我是用户ubuntu,所以~表示/home/ubuntu/

echo $GOBIN
   //空值
echo $GOPATH
   //空值
echo $GOROOT
   //空值
go env GOBIN
   //空值
go env GOPATH
/home/ubuntu/go
go env GOROOT
/usr/local/go
which go
/usr/local/go/bin/go

1)为什么go env给出的值与系统环境变量不同?我在文档中找不到任何关于此的说明,但我的假设是,如果环境变量在系统级别未设置,Golang会设置默认值-这些默认值会显示在go env中。这个假设正确吗?

2)为什么GOBIN未设置?我尝试显式设置系统环境变量,但在go env中没有反映出来,尽管系统环境变量似乎已被修改。为什么会这样?

echo $GOBIN
     //空值
go env GOBIN
     //空值
go env GOPATH
/home/ubuntu/go
GOBIN=‘/home/ubuntu/go/bin’
echo $GOBIN
/home/ubuntu/go/bin
go env GOBIN
     //空值

官方文档(https://pkg.go.dev/cmd/go)中提到:
> Env打印Go环境信息

但它没有提到这些变量的来源。


问题X(https://xyproblem.info/)

我正在尝试安装Delve(https://github.com/go-delve/delve),我的系统有两个go版本(go1.10.1go1.17.8),我计划使用后者(不幸的是无法删除前者)

go1.17.8 go install github.com/go-delve/delve/cmd/dlv@latest/home/ubuntu下创建一个新目录=> go,并在此处添加Delve。但是dlv version无法识别。
根据go help envGOBINgo install应该安装二进制文件的位置-在我的情况下它未设置,所以我猜想Golang会将其安装在GOPATH中。但即使如此,我也希望二进制文件能够被识别。我还尝试将位置添加到我的PATH中,但也没有起作用。

3)在通过go1.17.8进行安装之前,我应该将我的GOBIN设置为特定的值吗?
4)我的系统有两个go版本(which go指向go1.10.1版本),这会导致问题吗?(1.10.1不支持模块,但我尝试使用go.17.8进行安装,所以我认为这不会是个问题)

英文:

In my system, GOBIN variable was unset (empty value). Now unlike some other GO env variables, this seems unset even in the output for go env command. (I'm user ubuntu, so ~ is /home/ubuntu/)

echo $GOBIN
   //empty
echo $GOPATH
   //empty
echo $GOROOT
   //empty
go env GOBIN
   //empty
go env GOPATH
/home/ubuntu/go
go env GOROOT
/usr/local/go
which go
/usr/local/go/bin/go
  1. Why does go env give values different from the system environment variables? I couldn't find any documentation on this, but my assumption is that if env variables are not set at the system level, Golang sets default values - which are shown by go env. Is this assumption correct?

  2. Why is GOBIN unset? I tried setting the system env variable explicitly, but it doesn't get reflected in go env, even though the system env variable seems to get modified. Why is that so?

echo $GOBIN
     //empty
go env GOBIN
     //empty
go env GOPATH
/home/ubuntu/go
GOBIN=‘/home/ubuntu/go/bin’
echo $GOBIN
/home/ubuntu/go/bin
go env GOBIN
     //empty

The official documentation (https://pkg.go.dev/cmd/go) says:
> Env prints Go environment information

but it doesn't mention where the said variables are sourced from.


Problem X (https://xyproblem.info/)

I am trying to install Delve (https://github.com/go-delve/delve), and my system has two go versions (go1.10.1 and go1.17.8), and I plan to use the latter (unfortunately can't remove the former)

go1.17.8 go install github.com/go-delve/delve/cmd/dlv@latest makes a new directory at /home/ubuntu => go and adds Delve here. But dlv version is unrecognized.
From go help env, GOBIN is where go install should install binaries - in my case it's unset, so I'm guessing Golang installs it at GOPATH. But even then, I expected the binary to be recognized. I also tried adding the location to my PATH, but that didn't work either.

  1. Should I set my GOBIN to anything specific before installation via go1.17.8?
  2. Is my system having 2 go versions (which go is pointing to the go1.10.1 version), causing this? (1.10.1 doesn't have Modules support, but I was trying the install via go.17.8, so I assumed this wouldn't be an issue)

答案1

得分: 3

在Linux上,持久设置将存储在以下文件中:

$HOME/.config/go/env

通过使用go env -w命令进行设置。由于go工具从两者(即配置文件环境变量)加载设置,这就解释了为什么可能会看到不同的值。go工具将两者结合起来使用。

英文:

go on Linux will store persistent settings to this file:

$HOME/.config/go/env

via the go env -w command. Since the go tool loads settings from both this config and ENV VARS - this explains why you may see differing values. The go tool combines both.

答案2

得分: 3

这些 go env 变量是在安装二进制文件时设置的变量。

请参考 https://github.com/golang 的代码:

因此,这些变量不会出现在操作系统的环境变量中。

这些变量可以通过配置文件在 os.UserConfigDir 或通过命令 go env NAME=VALUE 进行覆盖。

英文:

These go env variables are variables set at the time of installation your binary.

Please refer the code of https://github.com/golang:

Thus, these variables do not appear as part of Operating systems environment variables.

These variables can be overridden by config file at os.UserConfigDir or by command go env NAME=VALUE.

huangapple
  • 本文由 发表于 2022年10月14日 10:28:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/74063696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定