Cannot find GOROOT directory: /usr/local/go - go

I have put this in / go / src /first_webapp server.go
And when I run the command go install first webapp I get this error,
cannot find GOROOT directory: /usr/local/go

Make sure you have installed Golang correctly and set your env variables like it says in here: https://golang.org/doc/install

Related

In an Eng Compute cluster - " cannot find GOROOT directory: /usr/lib/go"

I am trying to use GO in an engineering cluster. I have a private workspace, say /eng/name/go_proj/ ; how do I set the env var and the path to point to my workspace?
machine2045> pwd
/eng/name/go_proj/
machine2045> env | grep GOROOT
machine2045> which go
/usr/company_name/bin/go
machine2045> go env GOROOT
go: cannot find GOROOT directory: /usr/local/go
machine2045> go get -u github.com/go-delve/delve/cmd/dlv
go: cannot find GOROOT directory: /usr/local/go
I do not want to modify my .bashrc if possible since that is not recommended.
GOROOT (if not set manually via environment variable) defaults to /usr/local/go.
(EDIT: Well, it defaults to the parent directory of where the binary was compiled, but in many cases the end user is not compiling, they are downloading a precompiled binary... The point is that you need to be aware that there is a default GOROOT that will need to be overridden if you're using a non-standard directory for your go binary / installation).
Think of this as the parent folder of the bin folder that houses the go binary.
Go install instructions also encourage you to install to this location.
GOPATH specifies the root of your projects (to track sources and packages for imports, etc).
Think of this as the parent folder of the src, bin, and pkg folders used in go projects.
GOROOT should not be your workspace. GOPATH should.
If you don't want to modify your .bashrc or move your go installation to /usr/local/go, you should export a GOROOT and GOPATH variable manually, maybe in a launchmyproject.sh file:
export GOROOT=/usr/company_name/ && export GOPATH=/eng/name/go_proj/ && cd /eng/name/go_proj/ && vim main.go

Not using the $GOPATH specified in "go env"

I've got
export GOPATH=$HOME/Documents/go
in my .zshrc.
When trying to build a project with the following:
sudo go build -o /usr/bin/
I get a
main.go:5:2: cannot find package "github.com/foo/bar" in any of:
/usr/local/go/src/github.com/foo/bar (from $GOROOT)
/Users/JoahJoah/go/src/github.com/foo/bar (from $GOPATH)
I am assuming the "(from $GOPATH)" means the path is whatever I've set my $GOPATH variable to. And
go env
outputs the following:
GOPATH="/Users/Johannes/Documents/go"
The "Documents" part of the path seems to be omitted when using go build.
Is build using a GOPATH from somewhere else?
Since I was running the the command
$ sudo go build -o /usr/bin/
with sudo prefixed, it was running as the root user, causing a different $GOPATH to be used.
As Eric said, I should instead build to /usr/local/bin where sudo can be omitted.
$ go build /usr/local/bin
Works!

go: cannot find GOROOT directory: /usr/bin/go

Im trying to install a package but get the below problem even though my ROOT directory is set properly.
p#p-ubuntu:~/ba/docker-lvm-plugin$ which go
/usr/bin/go
p#p-ubuntu:~/ba/docker-lvm-plugin$ echo $GOROOT
/usr/bin/go
p#p-ubuntu:~/ba/docker-lvm-plugin$ go get github.com/Sirupsen/logrus
go: cannot find GOROOT directory: /usr/bin/go
If one issues go env then the following could be returned:
set GOPATH=C:\Users\user\go
set GOROOT=C:\tools\go
What is the GOPATH?
Create your workspace directory, %USERPROFILE%\go. (If you'd like to
use a different directory, you will need to set the GOPATH environment
variable; see How to Write Go Code for details.)
What is the GOROOT?
If you chose a directory other than c:\Go, you must set the GOROOT
environment variable to your chosen path.

Go installing package /usr/bin/go not a directory

Im trying to install a Go package but having the following error.
Here is my GOPATH
p#p-ubuntu:~/ba/docker-lvm-plugin$ echo $GOPATH
/usr/bin/go
Package install
p#p-ubuntu:~/ba/docker-lvm-plugin$ go get github.com/Sirupsen/logrus
package github.com/Sirupsen/logrus: mkdir /usr/bin/go: not a directory
Your GOPATH is wrong. It should not be the path to the go binary, it should be the path to the root of your go project directory, which should contain src, pkg, and bin subdirectories. See what should be the values of GOPATH and GOROOT?
This worked for me in Ubuntu:
Run in the terminal -
$ go env
Find what are the GOPATH and GOROOT paths mentioned in the output given by the above command.
Open settings.json in VsCode, To open settings in VsCode- use Ctrl+Shift+P and search for
Preferences: Open Settings (JSON)
After that set the following values
"go.gopath": "<paste GOPATH path found from the above terminal command>",
"go.goroot": "<paste GOROOT path found from the above terminal command>"
please change the GOBIN to available path, for example $GOPATH/bin, or $GOROOT/bin
you can see GOBIN with go env

Error "cannot download, $GOPATH not set."

Setup:
Have a look at my configuration:
$ echo $GOPATH && ls -r $GOPATH
/home/cyrus/.go
src pkg bin
$ echo $GOROOT && ls $GOROOT
/usr/local/go
api AUTHORS bin CONTRIBUTORS doc favicon.ico include lib LICENSE misc PATENTS pkg README robots.txt src test VERSION
You can see that I've set a path for $GOPATH. In addition, I've created subdirectories that I may not need.
Question:
Why does the following command generate this error message?
$ go get code.google.com/p/go-tour/gotour
package code.google.com/p/go-tour/gotour: cannot download, $GOPATH not set. For more details see: go help gopath
If you set a variable like this:
GOPATH=$HOME/go
It won't be exported to any subprocesses. It's only available to that process. If you want to export it to subprocesses, use export:
export GOPATH
You can also combine the assignment and export:
export GOPATH=$HOME/go
My $GOROOT directory was created with owner: root and group: wheel instead having me as the owner and admin as the group. I'll guess that I used sudo when I shouldn't have (or some tool did it for me). As such go get could not write the packages to $GOROOT and hence the $GOPATH not set and permission denied errors.
By using chown and chgrp on my $GOROOT (with -R to get subfolders) 'go get...' worked. In my case VS Code was now able to install the Go components I was after.
When you run go env, you should be able to see if your GOPATH has been set.
On my case though, this command solves it when I had it run on my terminal.
export GOPATH=/usr/local/bin
i had setup GOPATH and it showed correctly with command go env. i had to give the correct permissions to the go directory using chmod and install gb by go get github.com/constabulary/gb/....
First do a `go env' and check the path GOROOT. Accordingly set the GOPATH.
In my case, it had to set GOPATH to /usr/lib/go. Earlier i tried it with /usr/share/go and /usr/bin/go but it didn't work.
$GOPATH should be your current workspace , a folder where your project resides.
or just cd ~/go and try to run those commands.
Or you can just update $GOPATH in ~/.bashrc or ~/.bashrc_profile on ubuntu

Resources