goprotocol-buffersgrpcgrpc-gogrpc-gateway

Error in generated code with protoc-gen-grpc-gateway


I'm new to Protocol Buffers and gRPC stuff. Now I'm trying to build a client/server architecture with grpc + grpc-gateway in Go.

I tried to follow some examples but I always end up with the same problem. After generating the code with protoc i run go build and I get this error:

proto/helloworld/hello_world.pb.gw.go:64:2: cannot use msg (type *HelloReply) as type protoreflect.ProtoMessage in return argument:
        *HelloReply does not implement protoreflect.ProtoMessage (missing ProtoReflect method)
proto/helloworld/hello_world.pb.gw.go:98:2: cannot use msg (type *HelloReply) as type protoreflect.ProtoMessage in return argument:
        *HelloReply does not implement protoreflect.ProtoMessage (missing ProtoReflect method)

This is go.mod:

module github.com/riccardopedrielli/grpc-gateway-test

go 1.15

require (
    github.com/golang/protobuf v1.4.3
    github.com/grpc-ecosystem/grpc-gateway/v2 v2.2.0
    google.golang.org/genproto v0.0.0-20210207032614-bba0dbe2a9ea
    google.golang.org/grpc v1.35.0
    google.golang.org/protobuf v1.25.0
)

This is hello_world.proto:

syntax = "proto3";

package helloworld;

import "google/api/annotations.proto";

option go_package = "github.com/riccardopedrielli/grpc-gateway-test/proto/helloworld";

// Here is the overall greeting service definition where we define all our endpoints
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {
    option (google.api.http) = {
      get: "/v1/example/echo/{name}"
    };
  }
}

// The request message containing the user's name
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

This is the link to the repository: https://github.com/riccardopedrielli/grpc-gateway-test

A difference I see between the generated go files is that they are importing different protobuf libraries.
The one generated by protoc-gen-go imports github.com/golang/protobuf/proto.
The one generated by protoc-gen-grpc-gateway imports google.golang.org/protobuf/proto.
Could this be the cause of the problem?

Still it's not clear to me which one should be used and how to force the same in both the generators.

I'm new to grpc and quite lost at this point, so I could have omitted some important informations. Any suggestion will be welcomed.

Thank you


Solution

  • Ok I solved the issue.

    I had installed protoc via snap and the stable channel had version 3.11.4

    Now I upgraded to 3.14.0 and everything is working well.