找不到包 “google/protobuf”。

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

Cannot find package "google/protobuf"

问题

我正在编译一个用Go语言编写的开源项目(openblockchain)。当我执行go build时,出现以下错误。有人可以帮我解决这个问题吗?

编译错误

> go build
../go/src/github.com/openblockchain/obc-peer/openchain/util/utils.go:28:2: 找不到包“google/protobuf”,可能的位置有:
    /usr/src/pkg/google/protobuf(来自$GOROOT)
    /home/vichu/go/src/google/protobuf(来自$GOPATH)

附加信息

我在Stack Overflow上参考了这里的问题,但仍然没有解决这个问题。以下是我拥有的一些更多信息:

Protoc版本是最新的。

> protoc --version 
libprotoc 3.0.0

我的环境变量

> echo $GOPATH
/home/vichu/go
> echo $GOBIN
/home/vichu/go/bin

Protobuf已经使用README构建。

~/go/src/github.com/golang/protobuf$ ls
AUTHORS  CONTRIBUTORS  jsonpb  LICENSE  Makefile  Make.protobuf  proto  protoc-gen-go  proto.pb.go  ptypes  README.md

更新

我按照答案中提到的,在源代码的Util.go中进行了以下更改。
源代码是开源的,这是链接

-       gp "google/protobuf"
+       gp "github.com/google/protobuf"

当我执行go get时,出现以下错误

> go get github.com/google/protobuf
无法加载包:包github.com/google/protobuf中没有可构建的Go源文件,位于/home/vichu/go/src/github.com/google/protobuf
英文:

I am compiling an open source project written in go (openblockchain). I get the following error when I do go build. Can anyone help me with this issue

Compilation Error

> go build
../go/src/github.com/openblockchain/obc-peer/openchain/util/utils.go:28:2: cannot find package "google/protobuf" in any of:
	/usr/src/pkg/google/protobuf (from $GOROOT)
	/home/vichu/go/src/google/protobuf (from $GOPATH)

Additional Information

I referred the question here in Stack Overflow but still no luck in solving the issue. Here is some more information about what all I have:

Protoc version is up to date.

> protoc --version 
libprotoc 3.0.0

My environment variables

> echo $GOPATH
/home/vichu/go
> echo $GOBIN
/home/vichu/go/bin

Protobuf has been built using the README.

~/go/src/github.com/golang/protobuf$ ls
AUTHORS  CONTRIBUTORS  jsonpb  LICENSE  Makefile  Make.protobuf  proto  protoc-gen-go  proto.pb.go  ptypes  README.md

Update

I did the following Util.go in source code as mentioned in answer.
The source code is open source and here is the link

-       gp "google/protobuf"
+       gp "github.com/google/protobuf"

When I do go get, the below is the error

> go get github.com/google/protobuf
can't load package: package github.com/google/protobuf: no buildable Go source files in /home/vichu/go/src/github.com/google/protobuf

答案1

得分: 4

首先,你的导入语句是错误的,你试图导入一个 C++ 包,而不是一个 Golang 包。正确的导入语句应该是:

import "github.com/golang/protobuf/proto"

如果你还没有安装这个包,你需要在命令行中运行以下命令来安装:

go get github.com/golang/protobuf/proto
英文:

Firstly, your import is wrong, you are trying to import a C++ package, not a golang package. It needs to be:

import ("github.com/golang/protobuf/proto")

If you don't have this package installed already, you need to run from command line:

go get github.com/golang/protobuf/proto

答案2

得分: 0

我认为,在您的*.pb.go文件的末尾,您也没有类似于"gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00 ...."的内容。

问题是我们使用了错误的编译器进行生成。

所以:

1)我重新安装了protobuf,下载链接为:
https://github.com/google/protobuf/releases

2)然后(我使用的是Ubuntu,它也有一个proto编译器):
apt remove protobuf-compiler

重新构建*.proto文件。这样可以修复该错误。

英文:

I think, at the end of your *.pb.go file you also do not have some thing like "gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00 ...."

The issue is we use a wrong compiler for generating.

So:

  1. I reinstall protobuf from:
    https://github.com/google/protobuf/releases

  2. Then (I'm using ubuntu and it has a proto compiler also):
    apt remove protobuf-compiler

Rebuild the *.proto file. It fixed the bug.

huangapple
  • 本文由 发表于 2016年4月11日 13:26:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/36540427.html
匿名

发表评论

匿名网友

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

确定