What is gRPC?
- Go course
- Advanced
- Lesson 37/68
- 4 min
Learn the main advantages of gRPC and protobuf.
What is gRPC?
gRPC is a remote procedure call (RPC) framework developed by Google. A client calls a function on the server as if it were local. It uses HTTP/2 for transport and Protocol Buffers for data, so its messages are smaller and faster than JSON-based REST.
// on the client side
resp, err := client.Selam(ctx, &pb.SelamIstek{Ad: "Ada"})Four call types
gRPC offers four communication styles: unary (one request, one response), server streaming (one request, many responses), client streaming (many requests, one response) and bidirectional streaming. It is common for internal communication between microservices; using it directly from a browser needs an extra layer such as gRPC-Web.
Practice Simulating RPC
Imitate the essence of a gRPC call: type SelamServer struct{} with a method func (SelamServer) Selam(ad string) string returning "Merhaba " + ad. In main, print SelamServer{}.Selam("Ada").
Starter code
package main
import "fmt"
func main() {
// buraya yaz
}Sample solution
package main
import "fmt"
type SelamServer struct{}
func (SelamServer) Selam(ad string) string { return "Merhaba " + ad }
func main() {
fmt.Println(SelamServer{}.Selam("Ada"))
}Expected output
Merhaba Ada
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which transport protocol does gRPC use?
- FTP
- SMTP
- HTTP/2 Correct answer
- HTTP/1.0
Explanation: HTTP/2 provides multiple streams and bidirectional streaming over one connection; gRPC is built on it.
2 Data format
// selam.proto
syntax = "____";
// selam.proto
syntax = "proto3";
Explanation: Protocol Buffers is a binary, typed format; smaller and faster than JSON.
3 Call types
| Term | Meaning |
|---|---|
Unary |
1 request, 1 response |
Server streaming |
1 request, many responses |
Client streaming |
Many requests, 1 response |
Bidirectional |
Two-way stream |