goprotocol-buffersgrpcgrpc-go

How to fix redefined fields from protocol buffer definition in Go?


I'm building a gRPC client using the provider's .proto file and has a couple of enumerations that contains valued with the same name on it.

syntax = "proto3";

enum Color {
  NONE = 0;
  BLUE = 1;
}

enum Style {
  SOLID = 0;
  NONE = 1;
}

So when I generate the Go service for the .proto file and try and run it, I get the following error:

...\deal.pb.go:460:2: NONE redeclared in this block
...\deal.pb.go:105:2: other declaration of NONE

I tried moving the enumerations inside messages, like Color inside Shape, and expected this to provide a different name space. but did not work. Generated code declares blocks of constants and the message does not provide name spacing as I was hoping for. This worked for C#.

const (
  NONE Shape = 0
  BLUE Shape = 1
)

Any ideas on how to fix this?


Solution

  • You should rename the enum values because the primary problem in the provided code is that the generated Go code contains two constants with the name NONE, one from the Color enum and one from the Style enum. This causes a naming conflict because, in go, all constants from the generated protobuf code exist at the package level, making them global to the package. So, may be you can try this:

    enum Color {
      COLOR_NONE = 0;
      COLOR_BLUE = 1;
    }
    
    enum Style {
      STYLE_SOLID = 0;
      STYLE_NONE = 1;
    }