无法从$GOROOT和$GOPATH中找到软件包

kse8i1jr  于 2023-08-01  发布在  Go
关注(0)|答案(6)|浏览(201)

小部分的错误,我得到后,试图运行去构建命令

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输出

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不管,但仍然没有运气。有什么建议可以解决这个问题吗?

ux6nzvsh

ux6nzvsh1#

所有导入的包首先在GOROOT和GOPATH环境变量下查找。确保您的软件包位于这些目录下的某个位置。
现在假设GOPATH设置为:/Users/test/Desktop/GoProject

  • 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

4zcjmb1e

4zcjmb1e2#

正如官方文件所说:
最简单的方法是运行go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
在这里查看完整的介绍:golang/protobuf安装。

sd2nnvve

sd2nnvve3#

确保有一个版本的go 1.11+
旧版本不支持模块,因此无法下载所需的内容,导致Can't find package from $GOROOT and $GOPATH问题。

0sgqnhkj

0sgqnhkj4#

我在终端中安装了 go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
然后做了一个 go mod init example.com/filename,这创建了一个go mod文件,并为我删除了错误。

cpjpxq1n

cpjpxq1n5#

我使用Go 1.20,我的项目在~/DSBSystems。
下面是我运行的步骤:

  • go env -w GO111MODULE=on
  • cd ~/DSBSystems
  • go mod init DSBSystems
  • 去建造...
2hh7jdfx

2hh7jdfx6#

转到您的main.go目录并运行go get -u .,它将下载基于go.mod文件的代码所需的所有包

相关问题