我目前正在使用protobuf将基于rest api的go服务迁移到gRPC。 我正在使用互联网上的一些示例,我的service.proto文件就像
syntax = "proto3";
package v1;
import "google/protobuf/timestamp.proto";
// Taks we have to do
message ToDo {
// Unique integer identifier of the todo task
int64 id = 1;
// Title of the task
string title = 2;
// Detail description of the todo task
string description = 3;
// Date and time to remind the todo task
google.protobuf.Timestamp reminder = 4;
}
// Request data to create new todo task
message CreateRequest{
// API versioning: it is my best practice to specify version explicitly
string api = 1;
// Task entity to add
ToDo toDo = 2;
}
// Response that contains data for created todo task
message CreateResponse{
// API versioning: it is my best practice to specify version explicitly
string api = 1;
// ID of created task
int64 id = 2;
}
// Service to manage list of todo tasks
service ToDoService {
// Create new todo task
rpc Create(CreateRequest) returns (CreateResponse);
}
现在,在给定的代码段中,我们可以看到我们在同一.proto文件中定义了所有请求和响应。
我想在一个不同的go文件中定义它们,以便可以在整个项目中使用它们,例如-我有一个名为CreateRequest.go的模型文件,我可以以某种方式将其导入到此.proto文件中,其余的在该项目中,我也可以使用该CreateRequest模型,这样我就不必两次定义相同的模型。
1)可以这样做吗?
2)如果是,那么正确的语法是什么?
对此我是陌生的,因此,如果问题看起来很愚蠢,请大笑并忘记吧。