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

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

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

Can't find GOPATH even set env path

Using the root user installed go on linux.
Set go path in the ~/.zshrc file:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
$ ls $HOME/go
bin pkg src
$ ls $HOME/go/bin
asmfmt dlv errcheck fillstruct gocode gocode-gomod godef gogetdoc goimports golint gometalinter gomodifytags gorename gotags gounit guru iferr impl keyify motion
But go env returned:
zsh: command not found: go
Why?
Addition
$ whereis go
go: /usr/local/go
Set /usr/local to ~/.zshrc:
export PATH=$PATH:/usr/local/
Source it. Run go env returned:
zsh: permission denied: go
As commented, your ~/.zshrc should set PATH to reference Go, as seen in the Go wiki:
Edit your ~/.zshrc file to add the following line:
export GOPATH=$HOME/go
Save and exit your editor. Then, source your ~/.zshrc.
source ~/.zshrc
As explained in the default GOPATH and issue 17262, you don't need to set GOPATH.
But since Go 1.11 and its modules, you can now makes entire projects without ever using the default GOPATH at all: everything would remain local to your project.
As of Go 1.11, the go command enables the use of modules when the current directory or any parent directory has a go.mod, provided the directory is outside $GOPATH/src.
(Inside $GOPATH/src, for compatibility, the go command still runs in the old GOPATH mode, even if a go.mod is found)

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 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

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