proto语法.md 959 B

语法使用

repeated 修饰符 描述数组

syntax="proto3";

message ProdResponse {
    int32 prod_stock = 1;
}

message ProdStockList {
  repeated ProdResponse prodRes = 1;
}

枚举

syntax="proto3";
enum ProdArea{
  A=0;
  B=1;
  C=2;
}

message ProdRequest {
  int32 prod_id = 1;
  ProdArea prod_area = 2;
}

外部文件引用

syntax="proto3";

package  services;

option go_package = "/prod";

message ProdModel {
  int32 prod_id = 1;
  string prod_name = 2;
  float  prod_price = 3;
}
syntax="proto3";
package  services;
import "google/api/annotations.proto";
import "models.proto";

rpc GetProdInfo(ProdRequest)returns(ProdModel){}
 // 注意在同一包下时可以这样使用,如果models.proto中声明的package不是services包,则在使用时应该用 包名.ProdModel

字段验证库

https://github.com/bufbuild/protoc-gen-validate

流模式