Implementing a Unary gRPC Service
Server and client with grpc-go
What is a unary RPC?
A unary RPC is the simplest gRPC pattern: the client sends one request and receives one response, similar to a regular function call over the network.
Server implementation
Implement the generated interface. Embed the Unimplemented*Server struct for forward compatibility:
type userServer struct {
pb.UnimplementedUserServiceServer
db *sql.DB
}
func (s *userServer) GetUser(ctx context.Context, req *pb.GetUserRequest) (*pb.User, error) {
// query DB
return &pb.User{Id: req.Id, Name: "Alice"}, nil
}All lessons in this course
- Protocol Buffers and .proto Files
- Implementing a Unary gRPC Service
- Server and Client Streaming
- gRPC Interceptors and Metadata