protobuf教程(二)---引入其他proto文件

发布时间 2023-06-10 15:28:45作者: 若-飞

被引入的response.proto的文件:

syntax = "proto3";

package response;

option go_package = "github.com/TripleCGame/apis/api/response;response";
import "google/protobuf/struct.proto";

message Response {
  int32 code = 1;
  google.protobuf.Struct data = 2;
  string msg = 3; 
}

其中的package表明包名是response

syntax = "proto3";

package account.v1;

option go_package = "github.com/TripleCGame/apis/api/account/v1;v1";
import "google/api/annotations.proto";
import "response/response.proto";

service Account {
  rpc sendEmailCode (SendEmailCodeRequest) returns (response.Response) {
    option (google.api.http) = {
      post: "/account/sendEmailCode"
      body: "*"
    };
  }
}

message SendEmailCodeRequest {
  string email = 1;
  string type = 2;
}

当然首先倒入的路径需要在protoc中的proto_path中指定目录,用于查找:

protoc --proto_path=../../api \
--proto_path=../../pkg \
--proto_path=../../third_party \
--go_out=paths=source_relative:../../api \
--go-http_out=paths=source_relative:../../api \
--go-grpc_out=paths=source_relative:../../api \
--openapi_out=fq_schema_naming=true,default_response=false:. \
$(API_PROTO_FILES)

导入proto文件:

import "response/response.proto";

由于response.proto指定了包名叫response,所以引用的时候需要response.Response