
gRPC Tutorial for Beginners
8 min read
Step 0: What is Go? What is gRPC?
▶️ What is Go?
-
A fast, statically typed programming language made by Google.
-
Great for building servers, APIs, and tools.
-
Think of it as Python-level simplicity + C-like performance.
▶️ What is gRPC?
-
A high-performance Remote Procedure Call (RPC) framework.
-
Uses Protocol Buffers (proto files) instead of JSON.
-
Designed by Google — perfect for microservices and internal APIs.
-
gRPC lets apps call functions on other machines like calling local functions.
Step 1: Install Required Tools
Make sure Go is installed (go version
), then install:
- Protocol Buffer Compiler (protoc)
For Arch Linux
sudo pacman -Syu
sudo pacman -S protobuf
For Debian Based Linux Distributions
sudo apt update
sudo apt install -y protobuf-compiler
For macOS
brew install protobuf
- Go gRPC Plugins
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
Add Go binaries to your path if not already:
export PATH="$PATH:$(go env GOPATH)/bin"
Test:
protoc-gen-go --version
protoc-gen-go-grpc --version
Step 2: Project Structure
go-grpc-demo/
├── proto/
│ └── greeter.proto
├── server/
│ └── main.go
├── client/
│ └── main.go
├── go.mod
Initialize Go module:
go mod init go-grpc-demo
Step 3: Create the .proto
file
proto/greeter.proto
syntax = "proto3";
package greeter;
service GreeterService {
rpc SayHello (HelloRequest) returns (HelloResponse);
}
message HelloRequest {
string name = 1;
}
message HelloResponse {
string message = 1;
}
Step 4: Generate Go Code from Proto
protoc \
--go_out=. \
--go-grpc_out=. \
proto/greeter.proto
This creates:
proto/greeter.pb.go
proto/greeter_grpc.pb.go
These files contain:
-
The request/response structs
-
The server and client interfaces
Step 5: Implement the gRPC Server
server/main.go
package main
import (
"context"
"fmt"
"log"
"net"
"go-grpc-demo/proto"
"google.golang.org/grpc"
)
type server struct {
proto.UnimplementedGreeterServiceServer
}
func (s *server) SayHello(ctx context.Context, req *proto.HelloRequest) (*proto.HelloResponse, error) {
msg := fmt.Sprintf("Hello, %s!", req.Name)
return &proto.HelloResponse{Message: msg}, nil
}
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer()
proto.RegisterGreeterServiceServer(grpcServer, &server{})
log.Println("gRPC server running on port 50051")
if err := grpcServer.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
Step 6: Create the gRPC Client
client/main.go
package main
import (
"context"
"log"
"time"
"go-grpc-demo/proto"
"google.golang.org/grpc"
)
func main() {
conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
if err != nil {
log.Fatalf("failed to connect: %v", err)
}
defer conn.Close()
client := proto.NewGreeterServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
res, err := client.SayHello(ctx, &proto.HelloRequest{Name: "your name"})
if err != nil {
log.Fatalf("error calling SayHello: %v", err)
}
log.Printf("Server says: %s", res.Message)
}
Step 7: Run It
Open two terminals:
Terminal 1: Run the server
go run server/main.go
Terminal 2: Run the client
go run client/main.go
Output:
Server says: Hello, There!
Recap (Quick Mental Map)
-
.proto
file defines the API contract (messages + services) -
protoc
generates Go code from the.proto
definition -
Server implements the auto-generated interface
-
Client uses the auto-generated stub to call remote methods
Next Steps
-
Add TLS security to gRPC
-
Stream responses (server-side streaming)
-
Use interceptors (like middleware)
-
Dockerize the setup
More Articles
Key Words:
GoGoLangAPI GIN