【问题标题】:Cant find package from $GOROOT and $GOPATH无法从 $GOROOT 和 $GOPATH 找到包
【发布时间】:2017-04-10 13:48:58
【问题描述】:

我在尝试运行 go build 命令后遇到的一小部分错误

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)

去环境输出

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"

GOPATHGOROOT 已设置,但仍无法正常运行命令。尝试安装到更改 gopath 和 goroot 的主目录和自定义目录,但仍然没有运气。有什么建议可以解决这个问题吗?

【问题讨论】:

  • 你安装了protobuf吗?输入go get -u github.com/golang/protobuf/{proto,protoc-gen-go} 进行安装!
  • 如果我尝试使用该命令会出现同样的错误
  • 同样的错误?当go installgo buildgo install 之后?

标签: go gopath


【解决方案1】:

所有导入的包都先在 GOROOT 和 GOPATH 环境变量下查看。确保您的包位于这些目录下的某个位置。

现在假设 GOPATH 设置为:/Users/test/Desktop/GoProject

and GOROOT : /usr/local/go (go 安装在哪里) .如果您的 GoProject 中的文件具有导入为的包

import "abc/def/packageName"

那么它应该出现在以下两个地方:

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

如果没有,您将收到问题中报告的错误。

这些目录中的文件的第一行将是

package packageName

声明所有这些文件构成一个包packageName

【讨论】:

    【解决方案2】:

    正如官方文档所说:

    最简单的方法是运行go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

    在这里查看完整介绍:golang/protobuf installation

    【讨论】:

    • 也许你已经安装了protoc,protoc是.proto文件的编译器,而不是golang包如果你想使用protobuf包,请使用Github中的go get安装它!跨度>
    【解决方案3】:

    确保拥有 go 1.11+ 的版本。

    旧版本不支持模块,因此无法下载所需的内容,从而导致Can't find package from $GOROOT and $GOPATH 问题。

    【讨论】:

    • 使用 Ubuntu 18.04.4 LTS,我关注了these steps,问题就消失了。
    【解决方案4】:

    我在终端中安装了 go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

    然后做了一个 go mod init example.com/filename,这创建了一个 go mod 文件并为我消除了错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 2014-03-24
      相关资源
      最近更新 更多