I want to install json2csv using go get github.com/jehiah/json2csv but I receive this error:
package github.com/jehiah/json2csv: cannot download, $GOPATH not set. For more details see: go help go path
Any help on how to fix this on MacOS?
[Update: as of Go 1.8, GOPATH defaults to $HOME/go, but you may still find this useful if you want to understand the GOPATH layout, customize it, etc.]
The official Go site discusses GOPATH and how to lay out a workspace directory.
export GOPATH="$HOME/your-workspace-dir/" -- run it in your shell, then add it to ~/.bashrc or equivalent so it will be set for you in the future. Go will install packages under src/, bin/, and pkg/, subdirectories there. You'll want to put your own packages somewhere under $GOPATH/src, like $GOPATH/src/github.com/myusername/ if you want to publish to GitHub. You'll also probably want export PATH=$PATH:$GOPATH/bin in your .bashrc so you can run compiled programs under $GOPATH.
Optionally, via Rob Pike, you can also set CDPATH so it's faster to cd to package dirs in bash: export CDPATH=.:$GOPATH/src/github.com:$GOPATH/src/golang.org/x means you can just type cd net/html instead of cd $GOPATH/src/golang.org/x/net/html.
Keith Rarick notes you can set GOPATH=$HOME to put Go's src/, pkg/ and bin/ directories right under your homedir. That can be nice (for instance, you might already have $HOME/bin in your path) but of course some folks use multiple workspaces, etc.
This one worked
Setting up Go development environment on Ubuntu, and how to fix $GOPATH / $GOROOT
Steps
mkdir ~/go
Set $GOPATH in .bashrc,
export GOPATH=~/go
export PATH=$PATH:$GOPATH/bin
Using brew
I installed it using brew.
$ brew install go
When it was done if you run this brew command it'll show the following info:
$ brew info go
go: stable 1.4.2 (bottled), HEAD
Go programming environment
https://golang.org
/usr/local/Cellar/go/1.4.2 (4676 files, 158M) *
Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/go.rb
==> Options
--with-cc-all
Build with cross-compilers and runtime support for all supported platforms
--with-cc-common
Build with cross-compilers and runtime support for darwin, linux and windows
--without-cgo
Build without cgo
--without-godoc
godoc will not be installed for you
--without-vet
vet will not be installed for you
--HEAD
Install HEAD version
==> Caveats
As of go 1.2, a valid GOPATH is required to use the `go get` command:
https://golang.org/doc/code.html#GOPATH
You may wish to add the GOROOT-based install location to your PATH:
export PATH=$PATH:/usr/local/opt/go/libexec/bin
The important pieces there are these lines:
/usr/local/Cellar/go/1.4.2 (4676 files, 158M) *
export PATH=$PATH:/usr/local/opt/go/libexec/bin
Setting up GO's environment
That shows where GO was installed. We need to do the following to setup GO's environment:
$ export PATH=$PATH:/usr/local/opt/go/libexec/bin
$ export GOPATH=/usr/local/opt/go/bin
You can then check using GO to see if it's configured properly:
$ go env
GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/usr/local/opt/go/bin"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.4.2/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.4.2/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"
Setting up json2csv
Looks good, so lets install json2csv:
$ go get github.com/jehiah/json2csv
$
What just happened? It installed it. You can check like this:
$ $ ls -l $GOPATH/bin
total 5248
-rwxr-xr-x 1 sammingolelli staff 2686320 Jun 9 12:28 json2csv
OK, so why can't I type json2csv in my shell? That's because the /bin directory under $GOPATH isn't on your $PATH.
$ type -f json2csv
-bash: type: json2csv: not found
So let's temporarily add it:
$ export PATH=$GOPATH/bin:$PATH
And re-check:
$ type -f json2csv
json2csv is hashed (/usr/local/opt/go/bin/bin/json2csv)
Now it's there:
$ json2csv --help
Usage of json2csv:
-d=",": delimiter used for output values
-i="": /path/to/input.json (optional; default is stdin)
-k=[]: fields to output
-o="": /path/to/output.json (optional; default is stdout)
-p=false: prints header to output
-v=false: verbose output (to stderr)
-version=false: print version string
Add the modifications we've made to $PATH and $GOPATH to your $HOME/.bash_profile to make them persist between reboots.
Watch a Video
In general, I always recommend this official video from Go to get a quick overview on the matter:
http://www.youtube.com/watch?v=XCsL89YtqCs
It's easier to be shown than to be told.
#jwfearn paraphrased the important part of the video:
export GOPATH="${HOME}/gocode"; export PATH="${PATH}:${GOPATH}/bin"; mkdir -p "${GOPATH}"
I found easier to do it like this:
export GOROOT=$HOME/go
export GOPATH=$GOROOT/bin
export PATH=$PATH:$GOPATH
For MAC this worked well for me.
sudo nano /etc/bashrc
and add the below at the end of the file
export PATH=$PATH:/usr/local/opt/go/libexec/bin
export GOPATH=/usr/local/opt/go/bin
This should fix the problem. Try opening a new terminal and echo $GOPATH you should see the correct value.
(for MAC)
I tried all these answers and, for some still unknown reason, none of them worked.
I had to "force feed" the GOPATH by setting the environment variable per every command that required it. For example:
sudo env GOPATH=$HOME/goWorkDirectory go build ...
Even glide was giving me the GOPATH not set error. Resolved it, again, by "force feeding":
I tried all these answers and, for some still unknown reason, none of them worked.
I had to "force feed" the GOPATH by setting the environment variable per every command that required it.
sudo env GOPATH=$HOME/goWorkDirectory glide install
Hope this helps someone.
Your $GOROOT should not be set up.
You $GOPATH should be set to $HOME/go by typing export $GOPATH=$HOME/go
Please type export GOROOT="" to fix your problem.
Just do export GOPATH="/whatever/you/like/your/GOPATH/to/be".
If you run into this problem after having $GOPATH set up, it may be because you're running it with an unsupported shell. I was using fish and it did not work, launching it with bash worked fine.
You can use the "export" solution just like what other guys have suggested. I'd like to provide you with another solution for permanent convenience: you can use any path as GOPATH when running Go commands.
Firstly, you need to download a small tool named gost : https://github.com/byte16/gost/releases . If you use ubuntu, you can download the linux version(https://github.com/byte16/gost/releases/download/v0.1.0/gost_linux_amd64.tar.gz).
Then you need to run the commands below to unpack it :
$ cd /path/to/your/download/directory
$ tar -xvf gost_linux_amd64.tar.gz
You would get an executable gost. You can move it to /usr/local/bin for convenient use:
$ sudo mv gost /usr/local/bin
Run the command below to add the path you want to use as GOPATH into the pathspace gost maintains. It is required to give the path a name which you would use later.
$ gost add foo /home/foobar/bar # 'foo' is the name and '/home/foobar/bar' is the path
Run any Go command you want in the format:
gost goCommand [-p {pathName}] -- [goFlags...] [goArgs...]
For example, you want to run go get github.com/go-sql-driver/mysql with /home/foobar/bar as the GOPATH, just do it as below:
$ gost get -p foo -- github.com/go-sql-driver/mysql # 'foo' is the name you give to the path above.
It would help you to set the GOPATH and run the command. But remember that you have added the path into gost's pathspace. If you are under any level of subdirectories of /home/foobar/bar, you can even just run the command below which would do the same thing for short :
$ gost get -- github.com/go-sql-driver/mysql
gost is a Simple Tool of Go which can help you to manage GOPATHs and run Go commands. For more details about how to use it to run other Go commands, you can just run gost help goCmdName. For example you want to know more about install, just type words below in:
$ gost help install
You can also find more details in the README of the project: https://github.com/byte16/gost/blob/master/README.md
Run 'go env' and see where your GOPATH is currently pointing towards. If you change to that directory, your 'go get..etc' command should work.
This problem occured to me in raspberry pi. I had logged in through VNC client
The problem persisted despite setting and exporting the GOPATH.
Then Ran the "go get" command without sudo and it worked perfectly.
I am using vim to edit my .bashrc file but you code use a gui editor such as gedit.
Steps:
Kindly subsitute /path/to/golang/projects below with your actual path location where you will store your golang projects.
Open .bashrc file in vim that is vim ~/.bashrc. Then add below lines at the end of the file.
# Setup Golang Development Environment ::
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
# Third party go libraries will live under "~/golib" directory
export GOPATH="$HOME/golib"
export PATH="$PATH:$GOPATH/bin"
# Where your golang project code lives
export GOPATH=$GOPATH:/path/to/golang/projects
Save the file and type source ~/.bashrc to refresh your terminal session.
Now try getting a package e.g. go get github.com/pilu/fresh and check your ~/golib/bin directory it should have fresh package in it.
Navigate to your /path/to/golang/projects and create three folders in there i.e. bin,src and pkg
Now place your project folder inside /path/to/golang/projects/src e.g. /path/to/golang/projects/src/myfancygolangprojectand you should be good to go. Put all your golang codebase in there mate.
I had to run an application as root (to open a webserver on port 80), this produced the error for me, because the sudo user has a different environment than the normal user, hence GOPATH was not set.
If someone else is having this problem, add -E to the command, this will preserve the user environment.
sudo -E go run main.go
For more infos see discussion here: Google Groups – GOPATH Problem
Related
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
If I had a compiled Golang program that I wanted to install such that I could run it with a bash command from anywhere on my computer, how would I do that? For example, in nodejs
npm install -g express
Installs express such that I can run the command
express myapp
and express will generate a file directory for a node application called "myapp" in whatever my current directory is. Is there an equivalent command for go? I believe now with the "go install" command you have to be in the directory that contains the executable in order to run it
Thanks in advance!
Update: If you're using Go 1.16, this answer still works, but go install has changed and is now the recommended method for installing executable packages. See Karim's answer for an explanation: https://stackoverflow.com/a/68559728/10490740
Using Go >= 1.11, if your current directory is within a module-based project, or you've set GO111MODULE=on in your environment, go get will not install packages "globally". It will add them to your project's go.mod file instead.
As of Go 1.11.1, setting GO111MODULE=off works to circumvent this behavior:
GO111MODULE=off go get github.com/usr/repo
Basically, by disabling the module feature for this single command, it will install to GOPATH as expected.
Projects not using modules can still go get normally to install binaries to $GOPATH/bin.
There's a lengthy conversation and multiple issues logged about this change in behavior branching from here: golang/go - cmd/go: go get should not add a dependency to go.mod #27643.
Starting with Go >= 1.16 the recommended way to install an executable is to use
go install package#version
For example, go install github.com/fatih/gomodifytags#latest.
Executables (main packages) are installed to the directory named by the GOBIN environment variable, which defaults to $GOPATH/bin or $HOME/go/bin if the GOPATH environment variable is not set. You need to add this directory to your PATH variable to run executables globally. In my case, I've added this line to my ~/.zshrc file. (if you are using bash, add it to the ~/.bash_profile file):
export PATH="$HOME/go/bin:$PATH"
Go team published a blog post about this change, here's the explanation quote:
We used to recommend go get -u program to install an executable, but this use caused too much confusion with the meaning of go get for adding or changing module version requirements in go.mod.
Refer to go install documentation for more details
As far as I know, there is no direct equivalent to npm install -g. The closest equivalent would not be go install, but go get. From the help page (go help get):
usage: go get [-d] [-f] [-fix] [-insecure] [-t] [-u] [build flags] [packages]
Get downloads and installs the packages named by the import paths,
along with their dependencies.
By default, go get installs binaries to $GOPATH/bin, so the easiest way to make those binaries callable from everywhere is to add that directory to your $PATH.
For this, put the following line into your .bashrc (or .zshrc, depending on which shell you're using):
export PATH="$PATH:$GOPATH/bin"
Alternatively, you could also copy or link the executables to /usr/local/bin:
ln -s $GOPATH/bin/some-binary /usr/local/bin/some-binary
Short solution for Linux users:
Use the go get command as usual
Add the following lines to .bashrc:
# This is the default GOPATH, you should confirm with the 'go env' command
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
Restart terminal or source it. Installed binaries will be available globally.
For Go v1.8+
go install package_name#latest
Caveat: this answer is outdated following the 2020 deprecation of go get. The solution presented here won't work with newer Go runtime installs.
The closest analogue of this in Go would be go get. By default, it will fetch a Go package from a supplied repository URL, and requires a $GOPATH variable to be set in your shell so that Go knows where to store the packages (and subsequently where to find them when compiling code depending on go get-ted packages).
Example syntax:
$ go get github.com/user/repo
The behaviour supplied by npm's -g flag is default, and packages installed using go get are normally available globally.
See go get --help for more information about the command.
As mentioned by #helmbert, adding your $GOPATH to your $PATH is useful if you're installing standalone packages.
if you are using zsh :
first: install your package using :
go install package#version
then , you edit your .zshrc file
nano ~/.zshrc
Add this line to the end of .zshrc file :
export PATH="$HOME/go/bin:$PATH"
last but not least :
source ~/.zshrc
then open a new terminal and execute your command :)
TL;DR at the bottom. I'm going to walk you through how I came to this conclusion, and why the more obvious solutions don't work.
Upon seeing this question, I thought "If I could set root's GOPATH=/usr, it would install things in /usr/bin/ and /usr/src!"
So I tried the obvious thing:
Add GOPATH=/usr to root's .bashrc.
And it worked!
Sort of.
Not really.
Turns out, sudo doesn't execute root's .bashrc. For "security" or something like that.
Do env_set or something in /etc/sudoers
Turns out, /etc/sudoers can only remove environment variables. There's no env_set directive.
(As far as I can find)
Dig through man sudoers.
Where does sudo get it's default set of environment variables from?
Well, the first one in the list is /etc/environment, so that's the one I used.
sudo echo "GOPATH=/usr" >> /etc/environment
sudo go get <repo>
Binaries will be put in /usr/bin, and sources will be put in /usr/src.
Running go as non-root will use GOPATH the "normal" way.
If you don't have go installed, you may use the gobinaries. it builds an on-demand binary of the project from github repo.
The command to install the go package would be:
curl -sf https://gobinaries.com/rakyll/hey | sh
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).
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/
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