Where does `go get` store what it downloads? - go

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

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

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.

Where does go download my dependencies if I do not specify GOPATH?

new go learner so this might be a trivial question. If I understand the documentation correctly, go had been using GOPATH in the beginning and every dependency and source code need to be in this GOPATH but after 1.1.1 GO switched gear to use Go Modules which is like other languages doing.
Now if I do not specify GOPATH at all in my bashrc/bash_profile or anywhere, I can now do go init mymod and write some code and simply run go build and it will download whatever depdendency package I am using without any problem. I have tried setting GOPATH and when GOPATH is set, the packages are downloaded to my GOPATH as expected, but without GOPATH, this still works. But I am not sure where the dependencies are downloaded.
Could you please explain to me what is happening here? Where the dependency pacakge are downloaded in this case?
Quoting from Command go: GOPATH environment variable:
If the environment variable is unset, GOPATH defaults to a subdirectory named "go" in the user's home directory ($HOME/go on Unix, %USERPROFILE%\go on Windows), unless that directory holds a Go distribution. Run "go env GOPATH" to see the current GOPATH.

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.

How to set GOPATH on MAC after installing go1.4.1.darwin-amd64-osx10.8.pkg

I have run go1.4.1.darwin-amd64-osx10.8.pkg to install go on my MAC. It install go in /usr/local/go/bin/go.
Can you tell me what should my GOPATH set to? I tried '/usr/local/go' and '/usr/local/go/bin/go'. But both does not seem to be the right path.
Thank you.
GOPATH is an environmental variable used to define the location of your workplace directory. It's used by the Go tools for various reasons.
For example:
go get -u github.com/nsf/gocode
will download the source code and place it at
$GOPATH/src/github.com/nsfs/gocode
Compile that source code and
place the binary at $GOPATH/bin
Place symbol and package information at $GOPATH/pkg/architecture/github.com/nsfs
The path is also used in other tools:
go build github.com/nsf/gocode
go install github.com/nsfs/gocode
github.com/nsfs/gocode in the above commands is resolved automatically to $GOPATH/src/github.com/nsfs/gocode and thus you can run these commands without actually being in your workplace (the point of $GOPATH)
The $GOPATH location for your workplace directory can be put anywhere on your machine, but it must minimally have 3 folders (because go get and other tools need these folders).
bin
pkg
src
This environmental variable can be set like any other environmental variable. If you're using go from the Terminal.app, you can set it by opening the file:
vi ~/.bashrc
and then setting it
export GOPATH=~/goworkplace
~/goworkplace is a location to the workplace directory with those 3 folders. It can be anywhere on your system, such as, ~/Development/goworkplace, ~/Desktop/goworkplace, as long as it has those 3 folders
For information, take a look at this: https://golang.org/doc/code.html
Try this
First you can check the golang is install or not. Run go env
After that you can show the list of go env variables..
then you check where you can install the go
after that set $GOPATH
like:- export GOPATH=/var/projects/go
and also set $GOBIN
like:- export =$GOPATH/bin

Resources