Run (install) go code when GOPATH is inferred - go

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.

Related

How can I change the location Go downloads dependencies to? [duplicate]

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

How to fix wrong variables "go env GOROOT"?

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

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

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.

About warning popup after gopls installation in vscode

I installed gopls using command set GO111MODULE=on and golang.org/x/tools/gopls#latest to use gopls.
After that, I restarted the program and whenever I write and save the source code, a warning window appears like the picture below.
I'm not sure what this warning means.
I am working on GOPATH and have all the packages I need.
But I don't know why i need a module here.
As mentioned in "GOPATH in the VS Code Go extension"
Out of the box, the extension uses the value of the environment variable GOPATH. From Go 1.8 onwards, if no such environment variable is set, then the default GOPATH as deciphered from the command go env is used.
Check if you have set go.gopath or go.inferGopath.
Check what the returned value of go env GOPATH is.
More generally, it is a good idea to initialize a module at the root of your project (wherever your project is, GOPATH or not)
cd /path/to/my/project
go mod init myproject
Some issues mentioned that same error message
You are neither in a module nor in your GOPATH.
Please see https://github.com/golang/go/wiki/Modules for information on how to set up your Go project.
Issue 36120 for instance said:
I believe this is because my GOPATH is a colon-separated string:
GOPATH="/Users/user/go:/Users/user/go-work"
But... that was fixed in CL 211304 and commit 74e303f in gopls v0.3.2.

Where does `go get` store what it downloads?

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

Resources