gRPC in Go: Creating Your First Server
gRPC is a high-performance, open-source framework for remote procedure calls. It uses Protocol Buffers (protobuf) for defining messages and services, making communication between client and server fast and efficient. In this tutorial, we’ll create a Calculator gRPC server in Go that performs addition.
Overview of the Steps
Before diving into the implementation, here’s a brief overview of what we’ll do (the steps are explained below with code):
- Install prerequisites — Make sure Go is installed, along with the Protocol Buffers compiler (
protoc) and the Go gRPC plugins (protoc-gen-goandprotoc-gen-go-grpc). These tools generate Go code from.protofiles automatically. - Create the
.protofile — Define the messages (AddRequestandAddResponse) and theCalculateservice with theAddRPC method. This file acts as a contract between client and server. - Generate Go code — Use the
protoccompiler to create Go structs and server interfaces from the.protofile. - Implement the server in Go — Create
server.goand implement theAddmethod that receives two numbers and returns their sum. - Enable TLS (optional) — Secure the server using TLS certificates. (The cert and key files can be removed if TLS is not enabled, and the necessary changes should be made accordingly.)
- Run the server — Start the gRPC server and listen on a TCP port for incoming requests.
- Test the server — Use a gRPC client (or tools like Postman, Evans CLI) to send requests and verify addition works as expected.
Step 1: Install Prerequisites
Before starting, ensure you have:
- Go installed (version 1.20+ recommended)
- Protocol Buffers compiler (
protoc) - Go plugins for gRPC:
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latestThese plugins generate Go code from your .proto files, so you don’t have to write boilerplate code manually.
Step 2: Create the .proto File
Create a file called calculator.proto:
syntax = "proto3";
package proto;
option go_package = "simplegrpcserver/proto/gen;gen";
// Request message for addition
message AddRequest {
int32 a = 1;
int32 b = 2;
}
// Response message with the sum
message AddResponse {
int32 sum = 1;
}
// Calculator service
service Calculate {
rpc Add(AddRequest) returns (AddResponse);
}Explanation:
Get asmanya’s stories in your inbox
Join Medium for free to get updates from this writer.
AddRequest contains the two numbers to add. AddResponse contains the sum. Calculate service defines the Add RPC method. This .proto file is the blueprint for your service.
Step 3: Generate Go Code
Run the protoc compiler to generate Go code:
protoc --go_out=. --go-grpc_out=. proto/calculator.protoThis creates Go structs (AddRequest, AddResponse) and interfaces for the Calculate service in the proto/gen folder.
{Do not edit these generated files manually.}
Step 4: Implement the Server
Create server.go:
package main
import (
"context"
"log"
"net"
pb "simplegrpcserver/proto/gen"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/reflection"
)
// Server implementing the Calculate service
type server struct {
pb.UnimplementedCalculateServer
}
// Add two numbers and log metadata
func (s *server) Add(ctx context.Context, req *pb.AddRequest) (*pb.AddResponse, error) {
md, _ := metadata.FromIncomingContext(ctx)
log.Println("Metadata received:", md)
sum := req.A + req.B
return &pb.AddResponse{Sum: sum}, nil
}
func main() {
port := ":50051"
cert := "cert.pem"
key := "key.pem"
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatal("Failed to listen:", err)
}
creds, err := credentials.NewServerTLSFromFile(cert, key)
if err != nil {
log.Fatalln("Failed to load TLS credentials:", err)
}
grpcServer := grpc.NewServer(grpc.Creds(creds))
pb.RegisterCalculateServer(grpcServer, &server{})
reflection.Register(grpcServer)
log.Printf("Calculator gRPC server is running on port %s...", port)
if err := grpcServer.Serve(lis); err != nil {
log.Fatal("Failed to serve:", err)
}
}Key points:
- Implements the
Addmethod. - Logs any metadata sent by the client.
- Registers the service and starts the gRPC server with TLS.
Step 5: Run the Server
Start the server:
go run server.goYour server now listens on port 50051 and is ready to handle addition requests.
Step 6: Test the Server
You can test using a gRPC client or tools like Postman etc.
You now have a secure, functional Calculator gRPC server in Go!




