Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在.proto文件中正确导入go模型_Go_Protocol Buffers_Grpc_Grpc Go - Fatal编程技术网

如何在.proto文件中正确导入go模型

如何在.proto文件中正确导入go模型,go,protocol-buffers,grpc,grpc-go,Go,Protocol Buffers,Grpc,Grpc Go,我目前正在使用protobuf将基于RESTAPI的go服务迁移到gRPC。 我使用的是来自internet的一些示例,我的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 int6

我目前正在使用protobuf将基于RESTAPI的go服务迁移到gRPC。 我使用的是来自internet的一些示例,我的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) 如果是,正确的语法是什么

我不熟悉这个问题,所以如果这个问题看起来很愚蠢,那就笑一笑,忘掉它。

“一个名为CreateRequest.go的模型文件,我可以以某种方式将它导入到这个.proto文件中”-这不是方法。要使用proto文件, 1) 创建您的api.proto文件,不要忘记在其中添加一个类似“package apiv1”的包。 2) 使用protogen go将proto编译为api.pb.go 3) 创建一个“apichandler.go”文件并在该文件中导入apivi。因此,您正在将原始生成的包“apivi”导入到“apichandler.go”文件中

与其为请求和响应使用单独的.proto文件,还不如根据api版本或项目的任何合理组件将它们隔离开来。

“一个名为CreateRequest.go的模型文件,我可以以某种方式将其导入到这个.proto文件中”-这不是方法。要使用proto文件, 1) 创建您的api.proto文件,不要忘记在其中添加一个类似“package apiv1”的包。 2) 使用protogen go将proto编译为api.pb.go 3) 创建一个“apichandler.go”文件并在该文件中导入apivi。因此,您正在将原始生成的包“apivi”导入到“apichandler.go”文件中


与使用单独的.proto文件处理请求和响应不同,最好根据您的api版本或项目的任何合理组件对它们进行隔离。

反之亦然。你的真实来源是
*.proto
文件,你可以从这些文件生成你的Go文件。你可以定义一个
模型.proto
文件,将其导入主proto文件,然后在你的项目结构中的任何地方编译
模型.proto
文件。它的工作方式正好相反。你的真实来源是
*.proto
文件,你可以从这些文件生成你的Go文件。你可以定义
模型.proto
文件,将其导入主proto文件,并在项目结构中的任何地方编译
模型.proto
文件。