Home > Software engineering >  Why doesn't `go env` command reflect change in environment variable?
Why doesn't `go env` command reflect change in environment variable?

Time:10-14

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)

CodePudding user response:

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.

CodePudding user response:

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.

  • Related