安装json2csv时出现$ GOPATH错误

I'm running Linux 3.16.0-4-amd64 x86_64. I want to install json2csv.

I try:

sudo  go get github.com/jehiah/json2csv

and get:

package github.com/jehiah/json2csv: cannot download, $GOPATH not set. For more details see: go help gopath

go is installed:

GOARCH="amd64" 
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH=""
GORACE=""
GOROOT="/usr/lib/go"
GOTOOLDIR="/usr/lib/go/pkg/tool/linux_amd64" 
CC="gcc" 
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"
CXX="g++" 
CGO_ENABLED="1"

I set:

export GOROOT=/usr/bin/go
export GOPATH=$HOME/projects/go

but I still get the same error.

Any idea?

The reason is that you lost some of your ENV when you use sudo.
you could use -E to preserve it.

you can check it out here:
https://linux.die.net/man/8/sudo

But you should not use sudo to go get.

By the way, you could save you env in .bash_profile, so it can load automatically.


In this case, you should set GOROOT and GOPATH with

export GOROOT=/usr/local/go
export GOPATH=$HOME/projects/go

Mind that the GOROOT is the folder of go, no the binary of go.

then, go get without sudo.

go get github.com/jehiah/json2csv