protoc --gogofaster_out=. image.proto
我收到此错误消息
/home/abc/src/github.com/gogo/protobuf/gogoproto/gogo.proto:找不到档案。
image.proto:Import" /home/abc/src/github.com/gogo/protobuf/gogoproto/gogo.proto"没找到或有错误。
文件绝对存在。我可以从错误报告的路径中找到它。
我的proto文件如下所示:
package image;
import "/home/abc/src/github.com/gogo/protobuf/gogoproto/gogo.proto";
message Frame {
required bool fragment = 1;
required int32 fragmentID = 2;
required bool lastFragment = 3;
required bytes data = 4;
}
我在原型文件所在的目录中有一个Makefile
。看起来像这样:
regenerate:
--proto_path=../../github.com/gogo/protobuf/gogoproto:../../github.com/gogo/protobuf/protobuf/google/protobuf:. --gogofaster_out=. *.proto
如何解决路径错误?
答案 0 :(得分:1)
作为in this issue,您可以尝试仅导入相对路径:
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
确保GOPATH
首先设置为/home/abc
。
我提到protobuf PR 241,其中makefile定义了要使用的PATH:
regenerate:
protoc-min-version --version="3.0.0"
--proto_path=$(GOPATH)/src/image:
$(GOPATH)/src/github.com/gogo/protobuf/protobuf:
$(GOPATH)/src/github.com/gogo/protobuf/protobuf/google/protobuf:
. --gogo_out=. *.proto
OP提到this thread,指出:
依赖项会调用“
google/protobuf/
”中的其他依赖项,因此这些依赖项也必须正确地存在于路径中。
这些错误非常具有误导性使用gogoproto扩展程序时,您应该使用
gogofast_out
gofast_out
仅适用于您未使用任何扩展程序但需要一些额外速度的情况gogofast_out
/gofast_out
没有副作用gogofaster_out
生成大多数字段nullable=false
。
答案 1 :(得分:0)
go get -u github.com/gogo/protobuf
将依赖项下载到本地目录 $GOPATH/pkg/mod
go mod vendor
来打包您的模块依赖项。执行这个命令后,你的项目中应该有一个目录“vendor”,并在Makefile中引用。Makefile 命令应该是:
regenerate:
protoc-min-version --version="3.0.0" \
--proto_path=$(GOPATH)/src/image \
-I vendor/github.com/gogo/protobuf/protobuf \
-I vendor/github.com/gogo/protobuf/protobuf/google/protobuf \
-I. --gogo_out=. *.proto
这里,--pro_path 和 -I 是一样的。
如果绝对路径包含变量,则模式应为:
-I=${path_variable}/a/sample \
-I=${path_variable}/b/sample \
希望这可以帮助某人!