英文:
Protocol buffers in Go: cannot find package
问题
我正在按照Go语言的Protocol Buffer教程进行操作,但是遇到了以下问题:
- 我创建了addressbook的proto定义:
syntax = "proto3";
package tutorial;
message Person {
string name = 1;
...
}
- 我成功地运行了编译器并生成了Go代码。
- 我尝试导入pb包,但是失败了。
具体情况如下:我将--go_out
参数设置为与我的proto定义相同的目录(protoc --go_out=. addressbook.proto
),然后在同一个目录下创建了一个test.go文件,内容如下:
package main
import "tutorial"
但是运行go build test.go
时返回错误:
test.go:3:8: cannot find package "tutorial" in any of:
/usr/local/go/src/tutorial (from $GOROOT)
/home/vagrant/go2/src/tutorial (from $GOPATH)
然后我将test.go修改为:
package main
import "protobufs/tutorial"
但是又出现了以下错误:
test.go:3:8: cannot find package "protobufs/tutorial" in any of:
/usr/local/go/src/protobufs/tutorial (from $GOROOT)
/home/vagrant/go2/src/protobufs/tutorial (from $GOPATH)
但是如果我将导入语句修改为:
package main
import "protobufs"
它会发现在该位置有一个名为"tutorial"的包:
test.go:3:8: found packages tutorial (addressbook.pb.go) and main (list_people.go) in /home/vagrant/go2/src/protobufs
我做错了什么?为了使其工作,导入语句应该是什么样的?
谢谢!
我的go env的一部分信息如下:
GOARCH="amd64"
GOBIN="/home/vagrant/go2/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/vagrant/go2"
GORACE=""
GOROOT="/usr/local/go"
英文:
I'm following the Protocol Buffer for Go tutorial but I have the following problem:
- I create the addressbook proto definition
> syntax = "proto3";
> package tutorial;
>
> message Person {
> string name = 1;
> ...
> }
- I successfully run the compiler and generate the go code
- I try to import the pb package but it fails
Here is exactly what happens: I specifying the --go_out
to be same as my proto definition: (protoc --go_out=. addressbook.proto
)
then in same folder, I create a test.go with these simple lines:
package main
import "tutorial"
but go build test.go
returns error:
test.go:3:8: cannot find package "tutorial" in any of:
/usr/local/go/src/tutorial (from $GOROOT)
/home/vagrant/go2/src/tutorial (from $GOPATH)
then I change test.go
to this:
package main
import "protobufs/tutorial"
and get this error:
test.go:3:8: cannot find package "protobufs/tutorial" in any of:
/usr/local/go/src/protobufs/tutorial (from $GOROOT)
/home/vagrant/go2/src/protobufs/tutorial (from $GOPATH)
but if I change the import to only:
package main
import "protobufs"
it finds that there's a "tutorial" package in that location:
test.go:3:8: found packages tutorial (addressbook.pb.go) and main (list_people.go) in /home/vagrant/go2/src/protobufs
What am I doing wrong ? How should the import look like in order to make this work ?
Thank you !
FYI: a snippet of my go env:
GOARCH="amd64"
GOBIN="/home/vagrant/go2/bin"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/vagrant/go2"
GORACE=""
GOROOT="/usr/local/go"
答案1
得分: 2
这个问题显示了我对Go语言的包管理机制的理解不足。经过一些阅读,我得出了以下结论/规则:
- 每个文件夹对应一个包:文件夹"abc"中的所有.go文件将指定为
package abc
。 - 不能在同一个文件夹中同时存在
main
包和abc
包。 go install
命令会在$GOPATH/pkg/GOOS_GOARCH/<path_to_abc_excluding_abc>
目录下创建包对象abc.a
。- 对于位于
$GOPATH/src/x/y/z/foo/
文件夹中的main
包,go install
命令会编译并安装一个名为foo
(路径中最后一个目录的名称)的可执行文件到$GOPATH/bin
目录中。
现在,回到最初的问题:目录$GOPATH/src/protobufs
包含多个包:
- 编译后的protobuf包名为
tutorial
, test.go
中的main
包。
这与上述列出的规则相矛盾。
我认为一个优雅的解决方案是:
- 假设我在
$GOPATH/src/protobufs
目录下, - 创建一个名为
tutorials
的子目录, - 将编译后的protobuf安装到该子目录中:
protoc --go_out=./tutorial ./addressbook.proto
, - 现在,
test.go
可以使用package main
和import "protobufs/tutorial"
。
感谢你将我引导到正确的方向!
英文:
This question showed my lack of understanding of Go's packaging. After some reading, here are my conclusions/rules:
- one package per folder: all the .go files in directory "abc" will indicate
package abc
- you can't have package
main
and packageabc
in same folder go install
creates package objectabc.a
in$GOPATH/pkg/GOOS_GOARCH/<path_to_abc_excluding_abc>
- for the package
main
in folder$GOPATH/src/x/y/z/foo/
thengo install
compiles and installs an executable calledfoo
(the name of the last directory in the path) in$GOPATH/bin
Now, back to the initial question: the directory $GOPATH/src/protobufs
contains multiple packages:
- the compiled protobuf with the package name
tutorial
and - the
main
package intest.go
This contradicts with the above listed rules.
I believe that one elegant solution is:
- assuming I'm in
$GOPATH/src/protobufs
- create a subdir called
tutorials
- install the compiled protobuf in that subdir:
protoc --go_out=./tutorial ./addressbook.proto
- the
test.go
can now havepackage main
andimport "protobufs/tutorial"
Thanks for putting on the right track !
答案2
得分: 0
查看Go工作区:
工作区是一个目录层次结构,其根目录下有两个目录:
src:包含Go源文件
bin:包含可执行命令
本教程基于这个结构,即:
src/
github.com/protocolbuffers/protobuf/examples/
tutorial/
addressbook.pb.go
list_address.go
在该教程的makefile中,通过以下方式在examples
目录下生成pb.go
:
mkdir -p tutorial
protoc $$PROTO_PATH --go_out=tutorial addressbook.proto
在add_person.go中,它假设导入路径在workspace/src
下,即上面提到的$GOPATH/src
:
import (
....
pb "github.com/protocolbuffers/protobuf/examples/tutorial"
// 可以通过 $GOPATH/src/github.com/protocolbuffers/protobuf/examples/tutorial 找到
)
你需要做的是设置正确的GOPATH
并在$GOPATH/src
下生成pb.go
:
protoc -I=. --go_out=/Users/guihaoliang/Playground/go/my_workspace/src addressbook.proto
其中.
表示addressbook.proto
所在的位置。
GOPATH
工作区将被go modules取代,这样你就不必维护src
和bin
结构。protobuf示例不使用新的模块结构。
英文:
check GO workspace:
A workspace is a directory hierarchy with two directories at its root:
src contains Go source files, and
bin contains executable commands.
The tutorial is based on this structure, i.e.,
src/
github.com/protocolbuffers/protobuf/examples/
tutorial/
addressbook.pb.go
list_address.go
In the makefile of that tutorial, it generates pb.go
under examples
dir through:
mkdir -p tutorial
protoc $$PROTO_PATH --go_out=tutorial addressbook.proto
Under the add_person.go, it just assumes the import path is under worksapce/src
, which is $GOPATH/src
mentioned above:
import (
....
pb "github.com/protocolbuffers/protobuf/examples/tutorial"
// find through $GOPATH/src/github.com/protocolbuffers/protobuf/examples/tutorial
)
What you need to do is to set the right GOPATH
and generate that pb.go
under $GOPATH/src
protoc -I=. --go_out=/Users/guihaoliang/Playground/go/my_workspace/src addressbook.proto
where .
refers to where the addressbook.proto
is located.
The GOPATH
workspace is going to be superseded by go modules so that you don't have to maintain the src
and bin
structure. The protobuf example is not using the new module structure.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论