How to fix wrong variables "go env GOROOT"? - go

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

Related

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.

compile: version "go1.9" does not match go tool version "go1.9.1"

I am getting this error when I tried to run an example helloworld code I got onlie.
compile: version "go1.9" does not match go tool version "go1.9.1"
My computer has go1.9.1. What does the error mean and how can I fix this?
If you are installing using OSX homebrew you may need to set the $GOROOT in your .bashrc, .zshrc, etc:
export GOROOT=/usr/local/opt/go/libexec
I had the same error this morning when I updated from 1.9 -> 1.9.1 though according to several post the $GOROOT shouldn't have to be set and I had not set it until today. This may be a bug?
Edit: not a bug, for more details see answer below.
This is a mismatch between the GOROOT environment variable and the default path to your go command. One or the other needs to be changed; the one that needs to be changed depends on the specific setup on your computer. You could determine this by updating your Go to the latest version using your preferred method, running either which go (on Linux/macOS/BSD) or where go (on Windows), and then checking which of the files listed has the newer timestamp.
Linux/macOS/BSD
To change the GOROOT to match the default path of your go command, run type go and strip off the /bin/go part at the end to yield the directory path containing your Go installation. Then, add it to your .bashrc or other appropriate init file like this:
export GOROOT=/path/to/go-installation
To instead change the go command path to match your GOROOT, add this to the bottom of your init file:
export PATH="${GOROOT}/bin:${PATH}"
Windows
To change the GOROOT to match the default path of your go command, run where go take the first line of output, and strip off the \bin\go.exe part at the end. Then, go to "Edit the system environment variables" in Settings, click "Environment Variables...", find the "GOROOT" variable and edit to read the path you created earlier.
To instead change the go command path to match your GOROOT, first save the output of echo %GOROOT%\bin. Then, go to "Edit the system environment variables" in Settings, click "Environment Variables...", and find the
find the "Path" row in the bottom pane, click New, put in the path you created earlier, and finally click Move Up until it's at the top.
All
You'll need to open up a new command prompt to see the effects.
in case you are using mac with homebrew, just run:
brew cleanup
to clean all the legacy package, this fixed my problem.
In mac OS , if you downloaded and installed go package without brew, running brew update commands will cause this problem to occur
for fix this problem you can do :
brew uninstall --ignore-dependencies go
uninstalling go from brew will fix problem
This error happens when you forgot to delete previous golang install ... just delete its directory ... so identify go install location ... on linux issue
type go
typical output is
go is hashed (/usr/local/go/bin/go)
so just remove its grandparent directory ( go install dir not just the go binary )
sudo rm -rf /usr/local/go # NOTE this is not /usr/local/go/bin/go
now just install go and you'll be fine
For M1 Mac, the following steps helped me!
Check for which go from VSCode Terminal and check from system terminal.
from vscode terminal
user#mac % which go
/usr/local/go/bin/go
from my mac terminal
user#mac % which go
/opt/homebrew/bin/go
Whichever corresponds to the GOROOT shown go env, keep it and delete the other one
user#mac % go env GOROOT
/usr/local/go
in this case
rm -rf /opt/homebrew/bin/go
close and reload the vscode and terminal
For Windows delete the GOROOT System variables in the Enviroment Variables and restart the PC.
if you use VsCode, you just add this in setting.json.
"go.goroot": "/usr/local/Cellar/go/1.x.x/libexec",
For me, it's caused by GOROOT env, using gotip before, change to brew version.
# curret go env
cat "$(go env GOENV)"
# make sure this is correct
go env GOROOT
# unset GOROOT if setted before
go env -u GOROOT
you may also want to set a proper GOROOT to match the go version.
In my case, I had a scripts that look like this:
[[ -s "$HOME/.gvm/scripts/gvm" ]] && source "$HOME/.gvm/scripts/gvm"
Hanging around in my .bashrc/.zshrc file from a previous installation of go / trying to use gvm.
Removing this and restarting terminal solved it for me.
I had the same issue when I used getgo to update my Go version from 1.19 to 1.20. In my case, getgo created a .bash_profile and set its own export path w/c is not consistent with what's in my .bashrc.
#my .bash_profile;
export PATH=$PATH:/home/user/.go/bin
export PATH=$PATH:/home/user/go/bin
#my .bashrc;
export GOROOT=/usr/local/go/
export GOPATH=$HOME/go
export PATH=$PATH:$/home/user/go/bin:$GOROOT/bin:$PATH
SOLUTION:
I just replaced my export PATH in bashrc w/
export PATH=$PATH:$/home/user/.go/bin:$GOROOT/bin:$PATH
<Note the '.go' change w/c is now consistent to what's in my .bash_profile>.
So whether source is ~/.bashrc or ~/.bash_profile, it will always point to the same path for Go. Hope this helps. I'm also new to Go and Ubuntu. I know how painful it is to get these variables right on your own.
Took a simple approach(Linux), I had different versions of Go installed in my system.
$ whereis go
helped me to find the available go runnables, removed all, installed a fresh one and ensured to create a link for this new Go runnable in one of the $PATH folder to ensure below gives the correct version of what installed now.
$ go version

Run (install) go code when GOPATH is inferred

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.

Go : go get $GOPATH error, when GOPATH is set

When running the go get command :
sudo go get github.com/go-sql-driver/mysql
I get the following error
package github.com/go-sql-driver/mysql: cannot download, $GOPATH not
set. For more details see: go help gopath
However $GOPATH is already set.
Running echo $GOPATH gives /Users/userxyz/Desktop/Code
Running go env gives
.....
GOPATH="/Users/userxyz/Desktop/Code"
...
GOROOT="/usr/local/go"
.....
I have already tried setting GOPATH as an environment variable by adding the following lines
export GOPATH="$HOME/Desktop/Code"
export PATH=$PATH:$GOPATH/bin
to the following files, alternatively
~/.profile (/etc/profile)
~/.bashrc
~/.bash_profile
sudo go get github.com/go-sql-driver/mysql
This runs go get under root user, which does not have $GOPATH set.
Just do:
go get github.com/go-sql-driver/mysql
Generally, do:
go get
in the project folder, and it will install all dependencies. The following will install dependencies mentioned in tests:
go get -t
You just need to drop the sudo.
Your environment variables are defined at the user level. if you do sudo go env you'll see that GOPATH is not set there
You don't need sudo. If the "Agreeing to the Xcode/iOS license requires admin privileges" stops you, just re-open Xcode and accept the license agreement of Xcode.
Just realized after writing the response that it's almost 4 years too late but I'll leave it up just in case there are others who might need the answers...
So it seems like you might have created the directory with sudo or with root privileges. If you try $go get with a regular user you get a permissions error and when you try it with sudo you get a GOPATH unset error. I've had this problem recently, and I solved it by simply changing the ownership of the directory.
export GOPATH=/directory/of/your/goproject
chown -R ubuntu#staff $GOPATH
cd $GOPATH && go get
Here's just so you have some env variables to double check with:
export GOPATH=/home/ubuntu/work export
PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Keep in mind that there shouldn't be a need for you to declare or change $GOROOT.
Finally, you might want to use [profile.d][1] to export env variables:
cat > /etc/profile.d/setgoenv.sh <<EOL
export GOPATH=/home/ubuntu/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
EOL
I hope that helped.
In my case, I had imported an incorrect GOPATH that did not exist and an incorrect GOROOT in my PATH
setting my export GOROOT=/opt/go then export GOPATH=$HOME/work, ensuring $HOME/work existed and had access for my user seems to have solved the problem for go get {whattoget}, then ensuring export GOPATH=$HOME/work
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin was in my ~/.profile helped. Nothing in this thread seemed to quite fit.
Going forward I might just start using Docker to build my go projects, as it's a bit of a pain, and the documentation and errors are lengthy but don't seem to explain what is wrong in any meaningful and clear way (GOROOT and GOPATH were both setup, but PATH GOROOT and GOPATH all seemed to be a little off).

Go install fails with error: no install location for directory xxx outside GOPATH

~/src/go-statsd-client> echo $GOPATH
/Users/me/gopath
~/src/go-statsd-client> echo $GOROOT
/usr/local/Cellar/go/1.1.1\
~/src/go-statsd-client> go install
go install: no install location for directory /Users/me/src/go-statsd-client outside GOPATH
No matter what structure the project is in this always fails with the same message. Go build works perfectly.
Here is my go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/me/gopath"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.1.1"
GOTOOLDIR="/usr/local/Cellar/go/1.1.1/pkg/tool/darwin_amd64"
CC="gcc"
GOGCCFLAGS="-g -O2 -fPIC -m64 -pthread -fno-common"
CGO_ENABLED="1"
This is on Mac OSX Mountain Lion and go was installed with homebrew.
For any OS X users and future me, you also need to set GOBIN to avoid this confusing message on install and go get
mkdir bin
export GOBIN=$GOPATH/bin
When you provide no arguments to go install, it defaults to attempting to install the package in the current directory. The error message is telling you that it cannot do that, because the current directory isn't part of your $GOPATH.
You can either:
Define $GOPATH to your $HOME (export GOPATH=$HOME).
Move your source to within the current $GOPATH (mv ~/src/go-statsd-client /User/me/gopath).
After either, going into the go-statsd-client directory and typing go install will work, and so will typing go install go-statsd-client from anywhere in the filesystem. The built binaries will go into $GOPATH/bin.
As an unrelated suggestion, you probably want to namespace your package with a domain name, to avoid name clashing (e.g. github.com/you/go-statsd-client, if that's where you hold your source code).
You are using go install on a directory outside the GOPATH folder.
Set your GOBIN env variable, or move src folder inside GOPATH.
GOPATH/
bin/
src/
go-statsd-client/
More info: GO BUILD Source code, line 296
You need to setup both GOPATH and GOBIN. Make sure you have done the following (please replace ~/go with your preferred GOPATH and subsequently change GOBIN). This is tested on Ubuntu 16.04 LTS.
export GOPATH=~/go
mkdir ~/go/bin
export GOBIN=$GOPATH/bin
The selected answer did not solve the problem for me.
You'll want to have 3 directories inside your chosen GOPATH directory.
GOPATH
/bin
/src
/someProgram
program.go
/someLibrary
library.go
/pkg
Then you'll run go install from inside either someProgram (which puts an executable in bin) or someLibrary (which puts a library in pkg).
I had this problem on Windows.
My problem was that my %GOPATH% environment variable was set to
C:\Users\john\src\goworkspace
instead of
C:\Users\john\src\goworkspace\
Adding the missing trailing slash at the end fixed it for me.
For what it's worth, here's my .bash_profile, that works well for me on a mac with Atom, after installing go with Homebrew:
export GOROOT=`go env GOROOT`
export GOPATH=/Users/yy/Projects/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOBIN
In my case (OS X) it was because I have set GOPATH to /home/username/go (as per the book) instead of /Users/username/go
I'm on Windows, and I got it by giving command go help gopath to cmd, and read the bold text in the instruction,
that is if code you wnat to install is at ..BaseDir...\SomeProject\src\basic\set, the GOPATH should not be the same location as code, it should be just Base Project DIR: ..BaseDir...\SomeProject.
The GOPATH environment variable lists places to look for Go code. On
Unix, the value is a colon-separated string. On Windows, the value is
a semicolon-separated string. On Plan 9, the value is a list.
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.
See https://golang.org/wiki/SettingGOPATH to set a custom GOPATH.
Each directory listed in GOPATH must have a prescribed structure:
The src directory holds source code. The path below src determines the
import path or executable name.
The pkg directory holds installed package objects. As in the Go tree,
each target operating system and architecture pair has its own
subdirectory of pkg (pkg/GOOS_GOARCH).
If DIR is a directory listed in the GOPATH, a package with source in
DIR/src/foo/bar can be imported as "foo/bar" and has its compiled form
installed to "DIR/pkg/GOOS_GOARCH/foo/bar.a".
The bin directory holds compiled commands. Each command is named for
its source directory, but only the final element, not the entire path.
That is, the command with source in DIR/src/foo/quux is installed into
DIR/bin/quux, not DIR/bin/foo/quux. The "foo/" prefix is stripped so
that you can add DIR/bin to your PATH to get at the installed
commands. If the GOBIN environment variable is set, commands are
installed to the directory it names instead of DIR/bin. GOBIN must be
an absolute path.
Here's an example directory layout:
GOPATH=/home/user/go
/home/user/go/
src/
foo/
bar/ (go code in package bar)
x.go
quux/ (go code in package main)
y.go
bin/
quux (installed command)
pkg/
linux_amd64/
foo/
bar.a (installed package object)
..........
if GOPATH has been set to Base Project DIR and still has this problem, in windows you can try to set GOBIN as Base Project DIR\bin or %GOPATH%\bin.
Careful when running
export GOPATH=$HOME
Go assume that your code exists in specific places related to GOPATH. So, instead, you can use docker to run any go command:
docker run -it -v $(pwd):/go/src/github.com/<organization name>/<repository name> golang
And now you can use any golang command, for example:
go test github.com/<organization name>/<repository name>
In windows, my cmd window was already open when I set the GOPATH environment variable.
First I had to close the cmd and then reopen for it to become effective.
On OSX Mojave 10.14, go is typically installed at /usr/local/go.
Hence, setup these ENVs and you should be good to go.
export GOPATH=/usr/local/go && export GOBIN=/usr/local/go/bin
Also, add these to your bash_profile or zsh_profile if it works.
echo "export GOPATH=/usr/local/go && export GOBIN=/usr/local/go/bin" >> ~/.bash_profile && source ~/.bash_profile

Resources