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)
Related
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
I'm trying to install Golang in a Bluehost shared server.
I've done the following on the server so far:
cd ~
wget https://storage.googleapis.com/golang/go1.3.1.linux-amd64.tar.gz
tar -xvf go1.3.1.linux-amd64.tar.gz
rm go1.3.1.linux-amd64.tar.gz
vi .bashrc
I set the .bashrc file with the local values I want Go to work with.
# Configuration for Go
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
Then I run:
source ~/.bashrc
cd ~/go/src
./all.bash
And all goes well until the testing:
warning: GOPATH set to GOROOT (/home1/username/go) has no effect
...
fork/exec /tmp/go-build667300507/cmd/addr2line/_test/addr2line.test: permission denied
FAIL cmd/addr2line 0.053s
? cmd/cgo [no test files]
...
After that every single test fails, because I don't have access to /tmp. I was thinking maybe there is a variable to set the tests some place I do have access to, but I haven't found it.
About GOPATH set to GOROOT (/home1/username/go) has no effect. I've ran:
echo $GOROOT
And it is not set. So whats going on?
You should add GOROOT to your .bashrc:
export GOROOT=$HOME/go1.3.1.linux-amd64/go
# or, depending how the archive was un-tar'd:
export GOROOT=$HOME/go
export PATH=$PATH:$GOROOT/bin
GOPATH should be set to a workspace, a folder where your projects will reside
export GOPATH=$HOME/goprojects
export PATH=$PATH:$GOPATH/bin
You will have src, bin and pkg in GOPATH.
Remember, the go1.3.1.linux-amd64.tar.gz archive is for using go directly (it has binaries pre-compiled).
Doing a all.bash makes sense only if you build go from sources, but in that case, you would need only the sources (go1.3.1.src.tar.gz).
I'm trying to set my $GOPATH variable to run some example code on my machine:
$ smitego-example go run main.go
main.go:5:2: cannot find package "github.com/#GITHUB_USERNAME#/smitego" in any of:
/usr/local/go/src/pkg/github.com/#GITHUB_USERNAME#/smitego (from $GOROOT)
($GOPATH not set)
$ smitego-example export $GOPATH=$HOME
-bash: export: `=/Users/#OSX_USERNAME#': not a valid identifier
Contents of github.com/#GITHUB_USERNAME#/smitego/smitego.go:
package smitego
How can I set my GOPATH so it works always and forever?
Update, as of Go 1.8: If you're installing Go 1.8 (released: Feb 2017) or later, GOPATH is automatically determined by the Go toolchain for you.
It defaults to $HOME/go on macOS (nee OS X) - e.g. /Users/matt/go/. This makes getting started with Go even easier, and you can go get <package> right after installing Go.
For the shell: (the manual method)
~/.bash_profile should contain export GOPATH=$HOME/go and also export PATH=$GOPATH/bin:$PATH. The use of the $ is important: make sure to note where I've used it (and where I have not).
For Sublime Text:
Sublime Text menu > Preferences > Package Settings > GoSublime > Settings: User
{
"shell": ["/bin/bash"],
"env": {"GOPATH": "/Users/#USERNAME#/go/"},
}
Make sure your GOPATH is not set to the full path of the package; just the root of your go folder where src, pkg, and bin reside. If you're not using GoSublime, I'd suggest installing that first.
The accepted answer didn't work for me. I investigated and found the cause: I am using zsh, not bash.
I need to add the following two lines to ~/.zshrc:
export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH
You don't put the $ prefix on a variable when you're assigning it, only when you're reading it.
export GOPATH=$HOME
To make this permanent, put the command in your .bash_profile.
That will work for Terminal shells. If you need to set environment variables that will affect GUI applications, see Environment variables in Mac OS X
Download and install Go tools
https://golang.org/doc/install
Setup Go workspace
mkdir $HOME/go && cd $HOME/go
mkdir bin pkg src
Setup Go environment
sudo vi ~/.bash_profile
export GOPATH=$HOME/go
PATH=$PATH:$GOPATH/bin
Test by creating, building and running a Go project
mkdir -p $GOPATH/src/github.com/todsul/hello
touch $GOPATH/src/github.com/todsul/hello/hello.go
go install
hello
The http://www.golang-book.com/guides/machine_setup#osx
only has instructions for setting the path on ~/.bashrc, not ~/.bash_profile which thanks to this thread was able to get my example file to build.
export GOPATH=$HOME
export PATH=$PATH:$GOPATH/bin
Other Mac users need to add the above to their ~/.bash_profile.
After installing go with brew or with package this solved my problem:
export GOROOT="/usr/local/go"
export GOPATH="$HOME/Documents/goWorkSpace"
export PATH="$HOME/Documents/goWorkSpace/bin:$PATH"
on macOS High Sierra Version 10.3.3, Go[go version go1.10.1 darwin/amd64] Installed here :
Added following on :~/.bashrc
export GOPATH=/usr/local/go
export PATH=$PATH:$GOPATH/bin
and then Go Works
People working with the latest macs and above Catalina version,
you guys need to update the .zshrc file instead of .bash.
Add the following two lines to ~/.zshrc:
export GOPATH=/Users/username/go
export PATH=$GOPATH/bin:$PATH
it should work.!!
This got change a while back, please refer to the link below to understand why .zshrc and not .bash_profile
https://eshop.macsales.com/blog/56921-moving-from-bash-to-zsh-terminal-changes-in-macos-catalina/
~/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
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