无法从$ GOROOT和$ GOPATH查找软件包 [英] Cant find package from $GOROOT and $GOPATH

查看:711
本文介绍了无法从$ GOROOT和$ GOPATH查找软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试运行go build命令后出现的错误的小部分

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)

转到环境输出

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

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?

推荐答案

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

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

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

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

GOROOT :/usr/local/go(安装go的位置) .如果您的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:

/用户/测试/桌面/GoProject /src/abc/def/packageName/* /usr/local/go /src/abc/def/packageName/*

/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

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

stating that all these files constitutes a package packageName

这篇关于无法从$ GOROOT和$ GOPATH查找软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆