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
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?
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.
Should I set my GOBIN to anything specific before installation via go1.17.8?
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)
These go env variables are variables set at the time of installation your binary.
Please refer the code of https://github.com/golang:
as per https://github.com/golang/go/blob/master/src/internal/buildcfg/cfg.go,
these variables are either taken from environment variables or default variables.
These default values are generated as per runtime https://github.com/golang/go/blob/master/src/cmd/dist/buildruntime.go#L48
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.
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
I have been informed to use go get <github-URL> to download a go program.
The command exists with no output and exit code 0.
On my system both $GOPATH and $GOROOT are unset.
Where did go store my downloaded program?
It stores in GOPATH. If you don't set it explicitly, it has default values (depending on your system).
https://go.dev/doc/code#ImportingRemote
Module dependencies are automatically downloaded to the pkg/mod subdirectory of the directory indicated by the GOPATH environment variable
This go language issue says that if the GOPATH environment variable is unset, then by default:
$HOME/go is used on *NIX
%USERPROFILE%\go is used on Windows
I made a mistake:
go env -w GOROOT=/Users/apple/Documents/testProject/GXB_be:/usr/local/go
I set a wrong path as a variable of go env.
This makes it almost impossible for me to use the go env command in the vscode terminal,so I cannot directly use this command to repair it.
% go env
go: cannot find GOROOT directory: /Users/apple/Documents/testProject/GXB_be:/usr/local/go
I refer to this document https://go.dev/doc/install , tried to reinstall go, and tried to restart the vscode terminal, or even the macbook, but it did not work.
I think this is a global problem, because it not only affects vscode, but also occurs on the terminal.
My environment:
go version go1.19.3 darwin/amd64
vscode 1.72.2 (Universal)
Now I have temporarily solved this problem by importing environment variables.
However, every time a new vscode terminal is created, the problem still exists.
% go env
go: cannot find GOROOT directory: /Users/apple/Documents/testProject/GXB_be:/usr/local/go
% export GOROOT=/usr/local/go
% go env
warning: GOPATH set to GOROOT (/usr/local/go) has no effect
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
...
How can I restore the environment?
Remove (or edit) /Users//Library/Application Support/go/env.
-- from Volker
I have been informed to use go get <github-URL> to download a go program.
The command exists with no output and exit code 0.
On my system both $GOPATH and $GOROOT are unset.
Where did go store my downloaded program?
It stores in GOPATH. If you don't set it explicitly, it has default values (depending on your system).
https://go.dev/doc/code#ImportingRemote
Module dependencies are automatically downloaded to the pkg/mod subdirectory of the directory indicated by the GOPATH environment variable
This go language issue says that if the GOPATH environment variable is unset, then by default:
$HOME/go is used on *NIX
%USERPROFILE%\go is used on Windows
I just installed Go extension for Vscode and set "go.inferGopath": true, as explained here.
With autobuild on save my code builds, GOPATH is inferred correctly.
Is there a way to run my code (go install) with the inferred GOPATH from vscode? When running any Go command from vscode terminal it uses system GOPATH from go env.
go install will always use the go environment settings, unless you modify the go toolchain. On Linux, you can use the env command to change environment variables for the execution of a single command; on Windows, you can use SET to change environment variables for only the current shell session.
I'm trying to install a Go package (https://github.com/jteeuwen/go-bindata#installation) by running this command
go get -u github.com/jteeuwen/go-bindata/...
I get this error:
package github.com/jteeuwen/go-bindata/...: cannot download, $GOPATH not set. For more details see: go help gopath
My GOPATH is set in the ordinary way. I understand this is caused by the -u command... how do I resolve this?
It depends on your OS, but make sure your GOPATH is set in your .bashrc (as in this question) or least set with
export GOPATH=/home/<username>/go
That way, other processes (like go get) will inherit that variable.
On Windows, you would declare that variable in your user environment variables.
To check what is already set ("Print Go environment information"):
go env
If the variable is already set, but a go get -u still fails, then this could be similar to issue 42:
Your issue is that you have installed go to a location that requires privilege escalation to install to.
Check also your GOROOT and where go is installed.