PPGoodMan
PPGoodMan

Reputation: 351

GO GRPC function stub definition issue

I'm studying gRPC and Golang and am new to both. In this tutorial there's this function:

func (s *routeGuideServer) GetFeature(ctx context.Context, point *pb.Point) (*pb.Feature, error) {
    ...
}

I don't understand what "(s *routeGuideServer)" part in this function stub doing.

I've learnt go functions and "(ctx context.Context, point *pb.Point)" is the input and "(*pb.Feature, error)" is the output as far as I understand.

What exactly is this "(s *routeGuideServer)" part? Any pointers towards an explanation would be appreciated.

Upvotes: 1

Views: 336

Answers (2)

Dansekongen
Dansekongen

Reputation: 338

In GRPC when you make your protofile, the services you declare becomes an interface for the server.

You need to adhere to this interface, which means you need a struct that have these methods.

So the function that starts the server takes in an interface for the server, that requires all the rpc calls you defined under services. So to adhere to this, a struct is needed with all the methods.

If structs and methods were not a part of the language, the function would need to take in each function individually.

One big benefit is that when you initialize the struct, it can have fields, such as a database connection, which in return is available in each method.

Upvotes: 1

John Corry
John Corry

Reputation: 1577

It's the function receiver.

https://go.dev/tour/methods/4

It's kind of like a class in OOP languages, where the type (*routeGuideServer) has a function (or, method).

So in this case, the *routeGuideServer has a GetFeature function. You'll need a value of type *routeGuideServer to call that function.

Upvotes: 3

Related Questions