Back to Blog
System Design

Building Microservices with Go and gRPC

ARAtiqur Rahman
February 28, 2024

gRPC offers significant performance advantages over REST for inter-service communication. Let's build a simple Go microservice.

Defining the Protocol Buffer

First, we define our service interface and message types in a .proto file. This acts as a contract between client and server.

service.proto
syntax = "proto3";

package auth;

service AuthService {
  rpc Login (LoginRequest) returns (LoginResponse) {}
}

message LoginRequest {
  string email = 1;
  string password = 2;
}

message LoginResponse {
  string token = 1;
  int32 expires_in = 2;
}

Implementing the Server

We then generate the Go code and implement the interface.

main.go
type server struct {
    pb.UnimplementedAuthServiceServer
}

func (s *server) Login(ctx context.Context, req *pb.LoginRequest) (*pb.LoginResponse, error) {
    if req.Email == "admin" {
        return &pb.LoginResponse{Token: "TopSecret"}, nil
    }
    return nil, status.Error(codes.Unauthenticated, "Invalid credentials")
}
GoMicroservicesBackend

Command Palette

Search for a command to run...