简体   繁体   中英

Although GOPATH is set, echo $GOPATH returns nothing

I have this weird issue.

When I run go env , I get the following:

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/ethanc/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/ethanc/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/ethanc/marketstore/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build154822748=/tmp/go-build -gno-record-gcc-switches"

But when, I run echo $GOPATH , I get an empty new line. This happens for all other GO environment values as well when I try to echo them out: 空的 GOPATH 结果

This doesn't make sense because when I try to print out other path variables like echo $HOME , I get the correct value:

/home/ethanc

Does anyone have any idea why when I echo my GOPATH , only a new line is returned?

This is the default value for GOPATH.

Since it's never set, go will use it's reasonable default.

Main consquence of course is all your go projects sharing this $GOPATH.

Thank you @DonSimon ( https://stackoverflow.com/users/10453847/don-simon ) for pointing me in the right direction.

So, while my go environment variables were set up correctly and go env returned the correct env. variables, it wasn't set in my bash shell.

I just followed the instructions of setting the GOPATH automatically in my terminal: enter link description here

Specifically, I did this since I don't have .bash_profile :

  1. vi ~/.bashrc
  2. Added the following line of code at the bottom of the code: export GOPATH=$HOME/go

I think you can add the GOPATH in ~/.profile or ~/.bashrc

  • When you start bash as an interactive shell (ie, not to run a script), it reads ~/.bashrc

  • When invoked as a login shell, then it only reads ~/.bash_profile or ~/.profile . To understand more difference between shells, read this post

Attaching what I have in my ~/.profile here

export PATH=$PATH:/usr/local/go/bin
export PATH=$PATH:$HOME/go/bin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM