cannot download, $GOPATH not set

2019-01-07 05:00发布

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?

14条回答
趁早两清
2楼-- · 2019-01-07 05:15

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

查看更多
成全新的幸福
3楼-- · 2019-01-07 05:19

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

查看更多
Viruses.
4楼-- · 2019-01-07 05:22

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
5楼-- · 2019-01-07 05:23

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.

查看更多
Ridiculous、
6楼-- · 2019-01-07 05:25

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

查看更多
贪生不怕死
7楼-- · 2019-01-07 05:25

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.

查看更多
登录 后发表回答