go - Protoc 不创建服务

标签 go protocol-buffers proto protoc

我只是想使用 Protocol Buffer 为 golang 生成代码。但是无法使用 protoc 命令生成服务。

syntax = "proto3";
package greet;
option go_package="./greet/greetpb";
service GreetService{}
以上是我试图转换为 golang 的虚拟 proto 文件。但是无法生成正确的文件,因为在我的系统中它无法识别 service GreetService{} 线即服务。
我在 Windows 系统上使用以下版本的 protoc:
PS D:\Study\grpc\go-grpc-course> protoc --version
libprotoc 3.17.3
使用以下命令生成代码:

protoc .\greet\greetpb\greet.proto --go_out=.


下面是上面命令生成的go代码:
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
//  protoc-gen-go v1.26.0
//  protoc        v3.17.3
// source: greet/greetpb/greet.proto

package greetpb

import (
    protoreflect "google.golang.org/protobuf/reflect/protoreflect"
    protoimpl "google.golang.org/protobuf/runtime/protoimpl"
    reflect "reflect"
)

const (
    // Verify that this generated code is sufficiently up-to-date.
    _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
    // Verify that runtime/protoimpl is sufficiently up-to-date.
    _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)

var File_greet_greetpb_greet_proto protoreflect.FileDescriptor

var file_greet_greetpb_greet_proto_rawDesc = []byte{
    0x0a, 0x19, 0x67, 0x72, 0x65, 0x65, 0x74, 0x2f, 0x67, 0x72, 0x65, 0x65, 0x74, 0x70, 0x62, 0x2f,
    0x67, 0x72, 0x65, 0x65, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x05, 0x67, 0x72, 0x65,
    0x65, 0x74, 0x32, 0x0e, 0x0a, 0x0c, 0x47, 0x72, 0x65, 0x65, 0x74, 0x53, 0x65, 0x72, 0x76, 0x69,
    0x63, 0x65, 0x42, 0x11, 0x5a, 0x0f, 0x2e, 0x2f, 0x67, 0x72, 0x65, 0x65, 0x74, 0x2f, 0x67, 0x72,
    0x65, 0x65, 0x74, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}

var file_greet_greetpb_greet_proto_goTypes = []interface{}{}
var file_greet_greetpb_greet_proto_depIdxs = []int32{
    0, // [0:0] is the sub-list for method output_type
    0, // [0:0] is the sub-list for method input_type
    0, // [0:0] is the sub-list for extension type_name
    0, // [0:0] is the sub-list for extension extendee
    0, // [0:0] is the sub-list for field type_name
}

func init() { file_greet_greetpb_greet_proto_init() }
func file_greet_greetpb_greet_proto_init() {
    if File_greet_greetpb_greet_proto != nil {
        return
    }
    type x struct{}
    out := protoimpl.TypeBuilder{
        File: protoimpl.DescBuilder{
            GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
            RawDescriptor: file_greet_greetpb_greet_proto_rawDesc,
            NumEnums:      0,
            NumMessages:   0,
            NumExtensions: 0,
            NumServices:   1,
        },
        GoTypes:           file_greet_greetpb_greet_proto_goTypes,
        DependencyIndexes: file_greet_greetpb_greet_proto_depIdxs,
    }.Build()
    File_greet_greetpb_greet_proto = out.File
    file_greet_greetpb_greet_proto_rawDesc = nil
    file_greet_greetpb_greet_proto_goTypes = nil
    file_greet_greetpb_greet_proto_depIdxs = nil
}
后面的 go 代码中没有创建任何服务。我做错了什么还是这是一些原始问题。任何帮助表示赞赏。

最佳答案

你好像忘了加go-grpc_out使用时的参数 protoc .此参数用于生成 gRPC 代码。请尝试以下操作:

protoc greet.proto --go_out=. --go-grpc_out=.
这也在 the gRPC quickstart - section "Regenerate gRPC code" 中有所描述

关于go - Protoc 不创建服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68078115/

相关文章:

go - 在 http 和 mux 上使用 HandleFunc

java - 当您事先不知 Prop 体的消息类型是什么时,如何使用 Protocol Buffers 从 InputStream 进行解析?

javascript - 理解 typescript 生成的 __extends 函数?

go - 合并任意类型数组的惯用和DRY解决方案

google-app-engine - 如何修复Go 1.11模块未知导入路径,找不到提供包的模块?

php - python:在拦截器中读取grpc proto请求

proto - 如何将我的 Protobuf Schema 发布到 HERE OLP China?

protocol-buffers - 如何使用gRPC proto实现不同数据类型的动态自定义字段

go - 如何将 unsafe.Pointer 转换为 reflect.Type 或 reflect.Value

go - ./protoc-gen-go 慢吗?