1

I'm struggling to understand grpc in golang, the hello world example to be specific .

Specifically this line here:

// server is used to implement helloworld.GreeterServer.
type server struct {
    pb.UnimplementedGreeterServer
}

1 - Why is it embedding pb.UnimplementedGreeterServer as opposed to pb.GreeterServer?

2 - Why are there 2 .go files in the helloworld directory? Most examples I've looked at have one .go file and one .proto file.

nz_21
  • 6,140
  • 7
  • 34
  • 80
  • 2
    1.) Server must implement the interface `GreeterServer`, which requires some default methods provided by the embedded `pb.UnimplementedGreeterServer`. It's the easiest way for `server` to comply with the interface. 2.) Protobuf isn't exaclty the same as inter-process communication in GRPC. There's an additional file generated for that GRPC communication, utilizing the protobuf protocol. – NotX Mar 23 '21 at 19:03
  • @NotX if I replace it with GreeterServer instead it compiles fine. Was just curious because I've not seen any grpc projects that have the worst "UnimplementedXServer" in them – nz_21 Mar 23 '21 at 19:34
  • Tbh. I found the grpc.io documentation this example belongs to incredibly lacking when tried to set up my GRPC service back then. It it outdated at some places, and links to deprecated repositories, and it does a poor job at explaining which projects/plugins to use to generate an own service. So my guess would be: they just didn't put much love into that example as well. ;) – NotX Mar 23 '21 at 19:41

1 Answers1

1

For your first question, there is a long discussion here if you want to read, but TL;DR:

It is for forward-compatibility, that if you changed your service files and added some new methods, your binary doesn't fail if you don't implement the new methods in your server. Note that you can still use UnsafeGreeterServer if you don't want this behavior.

The reason that you are not seeing them often, is that this implementation added recently.

For the second question, this is the default behavior of command provided in go grpc quickstart, there are other commands that can generate only one file.

meshkati
  • 1,720
  • 2
  • 16
  • 29