无法从$GOROOT和$GOPATH找到包。

huangapple go评论104阅读模式
英文:

Cant find package from $GOROOT and $GOPATH

问题

运行go build命令后出现的错误的一小部分:

go build ./...
trillian.pb.go:7:8: 找不到包"github.com/golang/protobuf/proto",在以下任何位置都找不到:
    /usr/local/go/src/github.com/golang/protobuf/proto (来自$GOROOT)
    /Projects/Proj1/trillian/src/github.com/golang/protobuf/proto (来自$GOPATH)

trillian.pb.go:11:8: 找不到包"github.com/golang/protobuf/ptypes/any",在以下任何位置都找不到:
    /usr/local/go/src/github.com/golang/protobuf/ptypes/any (来自$GOROOT)
    /Projects/Proj1/trillian/src/github.com/golang/protobuf/ptypes/any (来自$GOPATH)

trillian_admin_api.pb.go:12:8: 找不到包"github.com/golang/protobuf/ptypes/empty",在以下任何位置都找不到:
    /usr/local/go/src/github.com/golang/protobuf/ptypes/empty (来自$GOROOT)
    /Projects/Proj1/trillian/src/github.com/golang/protobuf/ptypes/empty (来自$GOPATH)

trillian.pb.go:10:8: 找不到包"github.com/google/trillian/crypto/sigpb",在以下任何位置都找不到:
    /usr/local/go/src/github.com/google/trillian/crypto/sigpb (来自$GOROOT)
    /Projects/Proj1/trillian/src/github.com/google/trillian/crypto/sigpb (来自$GOPATH)

trillian_admin_api.pb.gw.go:17:2: 找不到包"github.com/grpc-ecosystem/grpc-gateway/runtime",在以下任何位置都找不到:
    /usr/local/go/src/github.com/grpc-ecosystem/grpc-gateway/runtime (来自$GOROOT)
    /Projects/Proj1/trillian/src/github.com/grpc-ecosystem/grpc-gateway/runtime (来自$GOPATH)

trillian_admin_api.pb.gw.go:18:2: 找不到包"github.com/grpc-ecosystem/grpc-gateway/utilities",在以下任何位置都找不到:
    /usr/local/go/src/github.com/grpc-ecosystem/grpc-gateway/utilities (来自$GOROOT)
    /Projects/Proj1/trillian/src/github.com/grpc-ecosystem/grpc-gateway/utilities (来自$GOPATH)

trillian_admin_api.pb.go:15:2: 找不到包"golang.org/x/net/context",在以下任何位置都找不到:
    /usr/local/go/src/golang.org/x/net/context (来自$GOROOT)
    /Projects/Proj1/trillian/src/golang.org/x/net/context (来自$GOPATH)

trillian_admin_api.pb.go:10:8: 找不到包"google.golang.org/genproto/googleapis/api/annotations",在以下任何位置都找不到:
    /usr/local/go/src/google.golang.org/genproto/googleapis/api/annotations (来自$GOROOT)
    /Projects/Proj1/trillian/src/google.golang.org/genproto/googleapis/api/annotations (来自$GOPATH)

trillian_log_api.pb.go:65:8: 找不到包"google.golang.org/genproto/googleapis/rpc/status",在以下任何位置都找不到:
    /usr/local/go/src/google.golang.org/genproto/googleapis/rpc/status (来自$GOROOT)
    /Projects/Proj1/trillian/src/google.golang.org/genproto/googleapis/rpc/status (来自$GOPATH)

trillian_admin_api.pb.go:11:8: 找不到包"google.golang.org/genproto/protobuf/field_mask",在以下任何位置都找不到:
    /usr/local/go/src/google.golang.org/genproto/protobuf/field_mask (来自$GOROOT)
    /Projects/Proj1/trillian/src/google.golang.org/genproto/protobuf/field_mask (来自$GOPATH)

Go环境输出:

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已设置,但仍然无法运行命令而不出现错误。尝试将其安装到主目录和自定义目录,并更改GOPATHGOROOT,但仍然没有成功。有没有什么建议如何解决这个问题?

英文:

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?

答案1

得分: 12

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

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

GOROOT:/usr/local/go(go安装的位置)。
如果你的Go项目中的文件导入了一个包:

import "abc/def/packageName"

那么它应该存在于以下两个位置之一:

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

如果不存在,你将会得到一个错误,就像问题中报告的那样。

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

package packageName

表示所有这些文件构成了一个名为packageName的包。

英文:

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

答案2

得分: 5

根据官方文档所说:

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

在这里查看完整的介绍:golang/protobuf 安装

英文:

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.

答案3

得分: 0

请确保安装了 Go 的 1.11+ 版本。

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

英文:

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.

答案4

得分: 0

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

然后执行了go mod init example.com/filename,这样就创建了一个go mod文件,并为我解决了错误。

英文:

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.

答案5

得分: 0

我使用的是Go 1.20版本,我的项目位于~/DSBSystems目录下。以下是我执行的步骤:

  • go env -w GO111MODULE=on
  • cd ~/DSBSystems
  • go mod init DSBSystems
  • go build ....
英文:

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

  • go env -w GO111MODULE=on
  • cd ~/DSBSystems
  • go mod init DSBSystems
  • go build ....

答案6

得分: 0

请前往你的main.go目录,并运行go get -u .。它将根据go.mod文件下载所有所需的包。

英文:

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

huangapple
  • 本文由 发表于 2017年4月10日 21:48:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/43325013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定