GOPATH正在寻找不同的道路

I have set GOPATH and GOROOT in ~/.bashrc as:

export GOROOT=/usr/lib/go-1.10

export GOPATH=/home/user/go/pkg

export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

and whenever I try to build with go build main.go I am getting.

main.go:4:2: cannot find package "google.golang.org/grpc/examples/grpcdemo/pb" in any of: /usr/lib/go-1.10/src/google.golang.org/grpc/examples/grpcdemo/pb (from $GOROOT) /home/user/go/src/google.golang.org/grpc/examples/grpcdemo/pb (from $GOPATH)

The question is why is looking inside home/user/go/src as I have set the GOPATH to /home/user/go/pkg. So, I have expected package inside/home/user/go/pkg/src/google.golang.org/grpc/examples/grpcdemo/pb. Shouldn't it be looking inside /home/user/go/pkg?

Probably should have

export GOPATH=/home/user/go

export PATH=$GOPATH/bin:$PATH
  • don't use GOROOT at all (do unset GOROOT just to be safe )
  • pkg is below GOPATH

Run $ go env and check if the output contains the followings or not:

  1. GOPATH="/home/user/go"
  2. GOROOT="/usr/local/go"

If not, then use like bellow in your ~/.bashrc file

export GOPATH=$HOME/go
# No need to set GOROOT since it is set /usr/local/go by default
export PATH=$GOPATH/bin:/usr/local/go/bin:$PATH

Now run $ source ~/.bashrc in bash. That's it.