在写proto文件生成pb文件脚本的时候,提醒github.com/mwitkow/go-proto-validators/validator.proto: File not found。为了可能更分明理解场景,先介绍一下我的项目文件目录,如下图所示。
上面抉择protobuf-spec文件下,任意一个proto文件为例来做展现和阐明问题。节选proto文件局部代码,代码如下:
syntax = "proto3"; package ofc.app; import "google/protobuf/empty.proto"; import "github.com/mwitkow/go-proto-validators/validator.proto"; //增加利用 message AppRequest{ int64 cid = 1; string appkey = 2; // @inject_tag: v:"required#利用名不能为空" string name = 3[(validator.field) = {string_not_empty: true}]; // @inject_tag: v:"required#行业类型不能为空" string industry_type = 4[(validator.field) = {string_not_empty: true}]; string info = 5; }
依据本机环境,编写script.sh脚本文件,依据proto生成pb文件:
# !bin/shproto_files=`find ./api/protobuf-spec -name "*.proto"`for proto_file in ${proto_files[*]}do `protoc -I. \ -I$GOPATH/pkg/mod/github.com/mwitkow/go-proto-validators@v0.3.2 \ -I$GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.14.6/third_party/googleapis \ --govalidators_out=./api/protobuf \ --go_out=plugins=grpc:./api/protobuf \ ${proto_file}`doneecho "success"
在执行script.sh的时候,提醒github.com/mwitkow/go-proto-validators/validator.proto: File not found,如下图所示。
依据谬误提醒,第一反馈就是go-proto-validators下载失败,进入文件目录查看,的确曾经存在,排除了这个问题之后就更困惑了。既然存在问啥还提醒这个谬误呢?依据文件门路找到了validator.proto文件。
关上"github.com/mwitkow/go-proto-validators/validator.proto"文件,代码如下:
看到了import "google/protobuf/descriptor.proto";在文件系统中的确没有找见这个文件,查找相干材料找到了对应的我的项目。
git clone https://github.com/protocolbuffers/protobuf
将"https://github.com/protocolbuffers/protobuf/" 中的 src/google 文件夹挪动到 %GOPATH%/src/google/上面,挪动过后,间接执行命令就能够了。
批改script.sh脚本,从新执行。
# !bin/shproto_files=`find ./api/protobuf-spec -name "*.proto"`for proto_file in ${proto_files[*]}do `protoc -I. \ -I$GOPATH/src \ -I$GOPATH/pkg/mod/github.com/mwitkow/go-proto-validators@v0.3.2 \ -I$GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-gateway@v1.14.6/third_party/googleapis \ --govalidators_out=./api/protobuf \ --go_out=plugins=grpc:./api/protobuf \ ${proto_file}`doneecho "success"
验证后果,如下图所示:github.com/mwitkow/go-proto-validators/validator.proto: File not found问题隐没。
对于go_package是另外一个问题,下一篇做具体介绍。