27

Small part of error I'm getting after trying to run go build command

go build ./...
trillian.pb.go:7:8: cannot find package "github.com/golang/protobuf/proto" in any of:
    /usr/local/go/src/github.com/golang/protobuf/proto (from $GOROOT)
    /Projects/Proj1/trillian/src/github.com/golang/protobuf/proto (from $GOPATH)

trillian.pb.go:11:8: cannot find package "github.com/golang/protobuf/ptypes/any" in any of:
    /usr/local/go/src/github.com/golang/protobuf/ptypes/any (from $GOROOT)
    /Projects/Proj1/trillian/src/github.com/golang/protobuf/ptypes/any (from $GOPATH)

trillian_admin_api.pb.go:12:8: cannot find package "github.com/golang/protobuf/ptypes/empty" in any of:
    /usr/local/go/src/github.com/golang/protobuf/ptypes/empty (from $GOROOT)
    /Projects/Proj1/trillian/src/github.com/golang/protobuf/ptypes/empty (from $GOPATH)

trillian.pb.go:10:8: cannot find package "github.com/google/trillian/crypto/sigpb" in any of:
    /usr/local/go/src/github.com/google/trillian/crypto/sigpb (from $GOROOT)
    /Projects/Proj1/trillian/src/github.com/google/trillian/crypto/sigpb (from $GOPATH)

trillian_admin_api.pb.gw.go:17:2: cannot find package "github.com/grpc-ecosystem/grpc-gateway/runtime" in any of:
    /usr/local/go/src/github.com/grpc-ecosystem/grpc-gateway/runtime (from  $GOROOT)
    /Projects/Proj1/trillian/src/github.com/grpc-ecosystem/grpc-gateway/runtime (from $GOPATH)

trillian_admin_api.pb.gw.go:18:2: cannot find package "github.com/grpc-ecosystem/grpc-gateway/utilities" in any of:
    /usr/local/go/src/github.com/grpc-ecosystem/grpc-gateway/utilities (from $GOROOT)
    /Projects/Proj1/trillian/src/github.com/grpc-ecosystem/grpc-gateway/utilities (from $GOPATH)

trillian_admin_api.pb.go:15:2: cannot find package "golang.org/x/net/context" in any of:
    /usr/local/go/src/golang.org/x/net/context (from $GOROOT)
    /Projects/Proj1/trillian/src/golang.org/x/net/context (from $GOPATH)

trillian_admin_api.pb.go:10:8: cannot find package "google.golang.org/genproto/googleapis/api/annotations" in any of:
    /usr/local/go/src/google.golang.org/genproto/googleapis/api/annotations (from $GOROOT)
    /Projects/Proj1/trillian/src/google.golang.org/genproto/googleapis/api/annotations (from $GOPATH)

trillian_log_api.pb.go:65:8: cannot find package "google.golang.org/genproto/googleapis/rpc/status" in any of:
    /usr/local/go/src/google.golang.org/genproto/googleapis/rpc/status (from $GOROOT)
    /Projects/Proj1/trillian/src/google.golang.org/genproto/googleapis/rpc/status (from $GOPATH)

trillian_admin_api.pb.go:11:8: cannot find package "google.golang.org/genproto/protobuf/field_mask" in any of:
    /usr/local/go/src/google.golang.org/genproto/protobuf/field_mask (from $GOROOT)
    /Projects/Proj1/trillian/src/google.golang.org/genproto/protobuf/field_mask (from $GOPATH)

Go env output

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/Projects/Proj1/trillian"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build690359699=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

GOPATH and GOROOT are set but still can't run the command without errors. Tried installing to home and custom directories changing gopath and goroot regardless but still no luck. Any suggestions how to solve this?

NatNgs
  • 874
  • 14
  • 25
Mantas
  • 271
  • 1
  • 3
  • 3

6 Answers6

12

All the packages imported are looked in under GOROOT and GOPATH environment variables first. Make sure your package is somewhere under these directories.

Now Suppose GOPATH is set to : /Users/test/Desktop/GoProject

and GOROOT : /usr/local/go (where go is installed) . If a file in your GoProject has a package imported as

import "abc/def/packageName"

then it should be present at any of the below two places:

/Users/test/Desktop/GoProject/src/abc/def/packageName/* /usr/local/go/src/abc/def/packageName/*

If not, you will get error as reported in the question.

The files inside these directories will have the first line as

package packageName

stating that all these files constitutes a package packageName

AvnishSingh
  • 488
  • 3
  • 10
5

As the official document says:

The simplest way is to run go get -u github.com/golang/protobuf/{proto,protoc-gen-go}.

See full introduction here :golang/protobuf installation.

nick
  • 843
  • 2
  • 7
  • 17
  • Maybe you have installed `protoc`,protoc is the compiler for `.proto` files, not golang packages i f you want to use protobuf package, install it using `go get` from `Github`! – nick Apr 10 '17 at 13:57
0

Make sure to have a version of go 1.11+.

Older versions do not support modules, thus they fail to download what's needed leading to the Can't find package from $GOROOT and $GOPATH problem.

Nicola
  • 395
  • 2
  • 13
  • With Ubuntu 18.04.4 LTS, I followed [these steps](https://tecadmin.net/install-go-on-ubuntu/) and the problem vanished. – Nicola Apr 16 '20 at 16:15
0

I installed go get -u github.com/golang/protobuf/{proto,protoc-gen-go}, in my terminal

Then did a go mod init example.com/filename, this created a go mod file and removed the error for me.

Hlobile S
  • 111
  • 1
  • 4
0

I used Go 1.20 and my project is at ~/DSBSystems.
Below are the steps I ran:

  • go env -w GO111MODULE=on
  • cd ~/DSBSystems
  • go mod init DSBSystems
  • go build ....
Nancy
  • 158
  • 2
  • 11
0

go to your main.go directory and run go get -u . It will download all the packages that is required for your code based on the go.mod file

GordonFreeman
  • 56
  • 1
  • 5