- Platform: YouTube
- Channel/Creator: Maximilien Andile
- Duration: 00:26:35
- Release Date: Nov 21, 2022
- Video Link: https://www.youtube.com/watch?v=gbrPMv_GuQY
Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.
This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.
- I summarize key points to help you learn and review quickly.
- Simply click on
Ask AIlinks to dive into any topic you want.
Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)
Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes
Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps
- Summary: Start by setting up a Go module for the project using
go mod initfollowed by the module path, likegithub.com/maximilienandile/demo-grpc. This creates ago.modfile with the module details and Go version. - Key Takeaway/Example: Use this to organize dependencies and ensure the project is modular from the beginning.
- Link for More Details: Ask AI: Initializing Go Modules
- Summary: Create a
.protofile to describe the API interface. Specifysyntax = "proto3";for protocol buffers version 3. Define a service likeinvoicerwith RPC methods such ascreatethat take request and response messages. Add messages for inputs (e.g., amount, currency, from, to) and outputs (e.g., PDF bytes). Include options likeoption go_packageto set the Go package path. - Key Takeaway/Example: Messages can nest other messages for reuse, like embedding an
amountmessage. Fields need types, names, and positions (e.g.,int64 amount = 1;).
syntax = "proto3";
option go_package = "github.com/maximilienandile/demo-grpc/invoicer";
service Invoicer {
rpc Create(CreateRequest) returns (CreateResponse);
}
message CreateRequest {
Amount amount = 1;
string from = 2;
string to = 3;
}
message Amount {
int64 value = 1;
string currency = 2;
}
message CreateResponse {
bytes pdf = 1;
}- Link for More Details: Ask AI: Defining Proto Files for gRPC
- Summary: Install
protoc(protobuf compiler) via instructions for your OS, likebrew install protobufon macOS. Runprotocwith options like--go_out=invoicer --go_opt=paths=source_relative --go-grpc_out=invoicer --go-grpc_opt=paths=source_relative invoicer.prototo generate Go code files (invoicer.pb.goandinvoicer_grpc.pb.go). - Key Takeaway/Example: After generation, use
go get -u google.golang.org/grpcto fetch dependencies, thengo mod tidyto clean upgo.mod. Regenerate code after any proto changes. - Link for More Details: Ask AI: Generating gRPC Code with Protoc
- Summary: In
main.go, set up a TCP listener withnet.Listen("tcp", ":8089"). Create a gRPC server withgrpc.NewServer(). Implement the service interface by defining a struct that embedsUnimplementedInvoicerServerand adds methods likeCreateto handle requests. Register the service withinvoicer.RegisterInvoicerServer(srv, &myInvoicerServer{})and start serving withsrv.Serve(lis). - Key Takeaway/Example: Handle errors for listening and serving. In the
Createmethod, access request fields and build responses.
package main
import (
"context"
"log"
"net"
"github.com/maximilienandile/demo-grpc/invoicer"
"google.golang.org/grpc"
)
type myInvoicerServer struct {
invoicer.UnimplementedInvoicerServer
}
func (s *myInvoicerServer) Create(ctx context.Context, req *invoicer.CreateRequest) (*invoicer.CreateResponse, error) {
return &invoicer.CreateResponse{Pdf: []byte(req.From)}, nil
}
func main() {
lis, err := net.Listen("tcp", ":8089")
if err != nil {
log.Fatalf("cannot create listener: %s", err)
}
srv := grpc.NewServer()
service := &myInvoicerServer{}
invoicer.RegisterInvoicerServer(srv, service)
err = srv.Serve(lis)
if err != nil {
log.Fatalf("impossible to serve: %s", err)
}
}- Link for More Details: Ask AI: Implementing gRPC Servers in Go
- Summary: Run the server with
go run main.go. Use a tool like BloomRPC to test: import the.protofile, set the address tolocalhost:8089, fill in JSON request examples, and send requests to verify responses. - Key Takeaway/Example: Responses show encoded bytes (e.g., ASCII values for strings). Add fields to proto, regenerate code, and test updates.
- Link for More Details: Ask AI: Testing gRPC Servers with BloomRPC
- Summary: Create a Makefile with targets like
generate-grpc-codeto automate code regeneration. Avoid editing generated files as changes are lost on regeneration. Add more RPC methods (e.g., update, delete) by updating the proto and regenerating. - Key Takeaway/Example: Protocol buffers handle encoding/decoding; gRPC manages requests/responses efficiently.
- Link for More Details: Ask AI: Best Practices for gRPC in Go
About the summarizer
I'm Ali Sol, a Backend Developer. Learn more:
- Website: alisol.ir
- LinkedIn: linkedin.com/in/alisolphp