douxin9135 2017-05-05 23:36
浏览 49

Go中的协议缓冲区:找不到包

I'm following the Protocol Buffer for Go tutorial but I have the following problem:

  1. I create the addressbook proto definition
syntax = "proto3";
package tutorial;

message Person {
  string name = 1;
...
}
  1. I successfully run the compiler and generate the go code
  2. 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条回答 默认 最新

  • duanjiao6730 2017-05-06 04:17
    关注

    This question showed my lack of understanding of Go's packaging. After some reading, here are my conclusions/rules:
    1. one package per folder: all the .go files in directory "abc" will indicate package abc
    2. you can't have package main and package abc in same folder
    3. go install creates package object abc.a in $GOPATH/pkg/GOOS_GOARCH/<path_to_abc_excluding_abc>
    4. for the package main in folder $GOPATH/src/x/y/z/foo/ then go install compiles and installs an executable called foo (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 in test.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 have package main and import "protobufs/tutorial"

    Thanks for putting on the right track !

    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计