0

I have a Golang service which uses Protobuf. The library used by Go and the version of the compiler need to be similar enough as per the documentation:

Users should use generated code produced by a version of protoc-gen-go that is identical to the runtime version provided by the protobuf module. This project promises that the runtime remains compatible with code produced by a version of the generator that is no older than 1 year from the version of the runtime used, according to the release dates of the minor version. Generated code is expected to use a runtime version that is at least as new as the generator used to produce it. Generated code contains references to protoimpl.EnforceVersion to statically ensure that the generated code and runtime do not drift sufficiently far apart.

The last sentence seems to imply that there is already code to ensure compatibility.

Is that correct?

That being said, I would like to be able to display the version of the library and of the compiled protobuf files in my logs to at least be able to manually verify that the versions are indeed compatible. How do I get both of these versions?


Update: Here is the section I mention in the comments.

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)
)
Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • 1
    Library major version is `1` and its minor version is the constant [`MaxVersion`](https://pkg.go.dev/google.golang.org/protobuf/runtime/protoimpl#pkg-constants). The version of the code generator is not exposed. – Pak Uula Nov 08 '22 at 19:35
  • @PakUula Oh! The page you refer me to [shows how it's verified](https://pkg.go.dev/google.golang.org/protobuf/runtime/protoimpl#EnforceVersion). I checked my .pb.go files and they have those two lines verifying the version. So in other words, the paragraph above does not lie. It's indeed verified. – Alexis Wilke Nov 08 '22 at 20:06
  • 1
    Yes, it is verified, but not exposed as a public constant. `protoimpl.EnforceVersion(20 - protoimpl.MinVersion)` -- the literal 20 is the codegenerator feature id. – Pak Uula Nov 08 '22 at 20:13

0 Answers0