登录
首页 >  Golang >  Go问答

无法使用 protoc-gen-go-grpc 正确生成代码

来源:stackoverflow

时间:2024-04-06 19:45:37 183浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《无法使用 protoc-gen-go-grpc 正确生成代码》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

这里是 golang 和 grpc 新人。我在 golang 中开始使用 protobufs 和 grpc 时遇到问题。我假设我在某个地方犯了一些愚蠢的错误。这是一些背景信息。

  • $gopath\bin 在我的 $path 中
  • 我已经安装了 protoc-gen-go 和 protoc-gen-go-grpc 插件
  • 我的 go_package 值指向公共 git 存储库。

我只是想复制 youtube 上名为 nic jackson 的视频 13 introduction to grpc and protocol buffers 中的内容,并从 .proto 文件生成一些 go 代码。

这是currency.proto的代码

syntax = "proto3";

option go_package = "github.com//go-modules;go-modules";

service currency {
    rpc getrate(raterequest) returns (rateresponse);
}

message raterequest {
    string base = 1;
    string destination = 2;
}


message rateresponse{
    float rate = 1;
}

不确定这是否有任何影响,但我的 go.mod 内容如下

module github.com//go-modules

go 1.15

项目的目录结构是

protos (root folder)
------server (folder)
------------currency.go
------currency.proto
------currency (folder)
------go.mod

我使用以下命令生成 go 代码。该命令在protos目录的上一级目录执行。

protoc -i protos\ protos\currency.proto --go-grpc_out=.\protos\currency

我收到以下错误三次

2021/01/13 20:52:28 warning: malformed 'go_package' option in "currency.proto", please specify:
        option go_package = "github.com//go-modules;go_modules";
a future release of protoc-gen-go will reject this.
see https://developers.google.com/protocol-buffers/docs/reference/go-generated#package for more information.

尽管出现错误,仍会生成一个名为currency_grpc.pb.go 的文件,并将其嵌套在货币文件夹下创建的 3 个新文件夹中。现在的布局是

currency
------ github.com
------------
------------------go-modules
------------------------currency_grpc.pb.go

currency_grpc.pb.go 的内容与视频中显示的内容完全不同(12:15 标记)。没有创建 raterequest 或 rateresponse 类型,因此破坏了代码。

我已经为此烦恼了几天,并且非常感谢您提供的任何帮助。我读过无数的 so 帖子和 github issues,但无法将它们拼凑在一起。如果需要的话,我们很乐意提供更多信息。

谢谢

编辑:我之前遗漏的 100 行生成的 go 代码。

// Code generated by protoc-gen-go-grpc. DO NOT EDIT.

package go_modules

import (
    context "context"
    grpc "google.golang.org/grpc"
    codes "google.golang.org/grpc/codes"
    status "google.golang.org/grpc/status"
)

// This is a compile-time assertion to ensure that this generated file
// is compatible with the grpc package it is being compiled against.
const _ = grpc.SupportPackageIsVersion7

// CurrencyClient is the client API for Currency service.
//
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type CurrencyClient interface {
    GetRate(ctx context.Context, in *RateRequest, opts ...grpc.CallOption) (*RateResponse, error)
}

type currencyClient struct {
    cc grpc.ClientConnInterface
}

func NewCurrencyClient(cc grpc.ClientConnInterface) CurrencyClient {
    return ¤cyClient{cc}
}

func (c *currencyClient) GetRate(ctx context.Context, in *RateRequest, opts ...grpc.CallOption) (*RateResponse, error) {
    out := new(RateResponse)
    err := c.cc.Invoke(ctx, "/Currency/GetRate", in, out, opts...)
    if err != nil {
        return nil, err
    }
    return out, nil
}

// CurrencyServer is the server API for Currency service.
// All implementations must embed UnimplementedCurrencyServer
// for forward compatibility
type CurrencyServer interface {
    GetRate(context.Context, *RateRequest) (*RateResponse, error)
    mustEmbedUnimplementedCurrencyServer()
}

// UnimplementedCurrencyServer must be embedded to have forward compatible implementations.
type UnimplementedCurrencyServer struct {
}

func (UnimplementedCurrencyServer) GetRate(context.Context, *RateRequest) (*RateResponse, error) {
    return nil, status.Errorf(codes.Unimplemented, "method GetRate not implemented")
}
func (UnimplementedCurrencyServer) mustEmbedUnimplementedCurrencyServer() {}

// UnsafeCurrencyServer may be embedded to opt out of forward compatibility for this service.
// Use of this interface is not recommended, as added methods to CurrencyServer will
// result in compilation errors.
type UnsafeCurrencyServer interface {
    mustEmbedUnimplementedCurrencyServer()
}

func RegisterCurrencyServer(s grpc.ServiceRegistrar, srv CurrencyServer) {
    s.RegisterService(&Currency_ServiceDesc, srv)
}

func _Currency_GetRate_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
    in := new(RateRequest)
    if err := dec(in); err != nil {
        return nil, err
    }
    if interceptor == nil {
        return srv.(CurrencyServer).GetRate(ctx, in)
    }
    info := &grpc.UnaryServerInfo{
        Server:     srv,
        FullMethod: "/Currency/GetRate",
    }
    handler := func(ctx context.Context, req interface{}) (interface{}, error) {
        return srv.(CurrencyServer).GetRate(ctx, req.(*RateRequest))
    }
    return interceptor(ctx, in, info, handler)
}

// Currency_ServiceDesc is the grpc.ServiceDesc for Currency service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
var Currency_ServiceDesc = grpc.ServiceDesc{
    ServiceName: "Currency",
    HandlerType: (*CurrencyServer)(nil),
    Methods: []grpc.MethodDesc{
        {
            MethodName: "GetRate",
            Handler:    _Currency_GetRate_Handler,
        },
    },
    Streams:  []grpc.StreamDesc{},
    Metadata: "currency.proto",
}

解决方案


使用protos作为根文件夹并不是一个好方法。我建议您仅为 .proto 文件创建一个 proto 文件夹。然后运行如下命令在同一个 proto 文件夹中生成 go 文件:

protoc proto/currency.proto --go_out=plugins=grpc:.

我在 github 上写了一篇关于 golang grpc 微服务的博客文章,其中包含简单的步骤和工作代码。您可以使用我在此使用的项目文件夹结构作为参考。这是我的博客文章的链接: http://softwaredevelopercentral.blogspot.com/2021/03/golang-grpc-microservice.html

好了,本文到此结束,带大家了解了《无法使用 protoc-gen-go-grpc 正确生成代码》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>