问题:

proto 

syntax = "proto3";
option go_package = ".;proto";
import "validate.proto";

service Greeter{
  rpc SayHello(Person) returns (Person);
}

message Person {
  uint64 id = 1 [(validate.rules).uint64.gt = 999];

  string email = 2 [(validate.rules).string.email = true];

  string name = 3 [(validate.rules).string = {
    pattern:   "[\u4e00-\u9fa5]",
    max_bytes: 30,
  }];

  Location home = 4 [(validate.rules).message.required = true];

  message Location {
    double lat = 1 [(validate.rules).double = {gte: -90,  lte: 90}];
    double lng = 2 [(validate.rules).double = {gte: -180, lte: 180}];
  }
}

proto文件生成对应go语言文件

 protoc -I ./  --go_out=. --validate_out="lang=go:." helloworld.proto

出现问题报错

google/protobuf/descriptor.proto: File not found.
google/protobuf/duration.proto: File not found.
google/protobuf/timestamp.proto: File not found.
validate.proto:7:1: Import "google/protobuf/descriptor.proto" was not found or had errors.
validate.proto:8:1: Import "google/protobuf/duration.proto" was not found or had errors.
validate.proto:9:1: Import "google/protobuf/timestamp.proto" was not found or had errors.
validate.proto:798:12: "google.protobuf.Duration" is not defined.
validate.proto:802:12: "google.protobuf.Duration" is not defined.
validate.proto:806:12: "google.protobuf.Duration" is not defined.
validate.proto:810:12: "google.protobuf.Duration" is not defined.
validate.proto:814:12: "google.protobuf.Duration" is not defined.
validate.proto:818:12: "google.protobuf.Duration" is not defined.
validate.proto:822:12: "google.protobuf.Duration" is not defined.
validate.proto:832:12: "google.protobuf.Timestamp" is not defined.
validate.proto:836:12: "google.protobuf.Timestamp" is not defined.
validate.proto:840:12: "google.protobuf.Timestamp" is not defined.
validate.proto:844:12: "google.protobuf.Timestamp" is not defined.
validate.proto:848:12: "google.protobuf.Timestamp" is not defined.
validate.proto:861:12: "google.protobuf.Duration" is not defined.
validate.proto:12:8: "google.protobuf.MessageOptions" is not defined.
validate.proto: "google.protobuf.MessageOptions" is not defined.
validate.proto:21:8: "google.protobuf.OneofOptions" is not defined.
validate.proto:28:8: "google.protobuf.FieldOptions" is not defined.
helloworld.proto:3:1: Import "validate.proto" was not found or had errors.

先去github上面下载需要的文件

Release Protocol Buffers v23.1 · protocolbuffers/protobuf · GitHub 

把需要的google文件 放入自己go_path的src目录下面

 

 

指定路径

protoc -I ./ -I "D:\Go\src" --go_out=. --validate_out="lang=go:." helloworld.proto

参考文章

Windows下protoc-gen-validate的安装使用_lwb拾光的博客-CSDN博客

gRPC集成protoc-gen-validate_.番茄炒蛋的博客-CSDN博客

https://www.cnblogs.com/yisany/p/14875488.html

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐