Skip to main content

Building Your First API in Go: REST vs. gRPC Compared

Welcome back to the KpolitX tech blog! In our first post, we built out a pristine development environment. Now, it is time to put that environment to work by building a foundational backend component: an API.

When building microservices or backend applications in Go, two primary architectural patterns dominate the conversation: REST (Representational State Transfer) and gRPC (Google Remote Procedure Call).



In this guide, we will break down the fundamental differences between them and look at how they perform in code.

1. The Core Philosophical Differences

  • REST (JSON over HTTP/1.1): REST has been the web standard for decades. It is resource-oriented, uses human-readable JSON payloads, and relies heavily on standard HTTP methods (GET, POST, PUT, DELETE). It is universally compatible with web browsers.
  • gRPC (Protocol Buffers over HTTP/2): Developed by Google, gRPC uses binary serialization (Protocol Buffers) and runs on HTTP/2. Instead of exposing web resources, it focuses on calling methods on a remote server as if they were local functions. It is built for raw speed, contract enforcement, and high-performance microservice communication.

2. Building a Simple REST API in Go

Go's standard library (net/http) makes spinning up a basic REST server remarkably straightforward without needing heavy external frameworks.

go
package main

import (
    "encoding/json"
    "net/http"
)

type Message struct {
    Status  string `json:"status"`
    Content string `json:"content"`
}

func handlePing(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    response := Message{Status: "success", Content: "Pong from Go REST API!"}
    json.NewEncoder(w).Encode(response)
}

func main() {
    http.HandleFunc("/ping", handlePing)
    println("REST API server running on port 8080...")
    http.ListenAndServe(":8080", nil)
}

3. Building a gRPC Service in Go

With gRPC, you define your service contract first using a .proto file, and then compile it into Go code using the Protocol Buffers compiler.

Step A: Define the Contract (service.proto)

protobuf
syntax = "proto3";

package api;
option go_package = "/api";

service PingService {
    rpc Ping (PingRequest) returns (PingResponse);
}

message PingRequest {
    string name = 1;
}

message PingResponse {
    string message = 1;
}

Step B: Implement the Server in Go

Once compiled, you implement the generated interface:

go
package main

import (
    context "context"
    "log"
    "net"

    pb "your_project/api"
    "google.golang.org/grpc"
)

type server struct {
    pb.UnimplementedPingServiceServer
}

func (s *server) Ping(ctx context.Context, req *pb.PingRequest) (*pb.PingResponse, error) {
    return &pb.PingResponse{Message: "Hello " + req.Name + " from gRPC!"}, nil
}

func main() {
    lis, err := net.Listen("tcp", ":50051")
    if err != nil {
        log.Fatalf("failed to listen: %v", err)
    }
    
    s := grpc.NewServer()
    pb.RegisterPingServiceServer(s, &server{})
    
    log.Println("gRPC server running on port 50051...")
    if err := s.Serve(lis); err != nil {
        log.Fatalf("failed to serve: %v", err)
    }
}

4. Performance & When to Use Which

When you benchmark them side-by-side:

  • Payload Size: gRPC uses binary serialization, making messages significantly smaller than verbose text-based JSON.
  • Speed & Throughput: HTTP/2 allows gRPC to multiplex multiple requests over a single TCP connection, drastically cutting down latency under high concurrency.

The Golden Rule: Use REST when building public-facing APIs or web applications where browser compatibility and human-readability are critical. Use gRPC for internal high-throughput communication between backend microservices.

Comments

Popular posts from this blog

How to Set Up .NET on Your PC: A Complete Beginner's Guid

Completely new to .NET? This beginner-friendly guide shows you how to install .NET, set up Visual Studio, configure your development environment, create your first C# project, understand the project structure, run your application, and take your first steps into ASP.NET Core development. Introduction If you've never worked with .NET before, getting started can feel confusing. You may see terms such as C# , .NET SDK , ASP.NET Core , Visual Studio , NuGet , and CLI everywhere. So instead of jumping directly into a complicated application, this guide starts from the beginning. We will set up a complete .NET development environment on a Windows PC, create our first C# application, understand the basic project structure, run the application, and finally create a simple ASP.NET Core Web API. You don't need previous .NET experience to follow this...

Setting Up the Ultimate Development Environment: From Zero to Code with KpolitX

 Welcome to the official KpolitX tech blog! Today, we are kicking things off by pulling back the curtain on how to set up a robust, efficient, and multi-language development environment from scratch. Whether you're working with systems languages like Go and Rust, or building full-stack web applications, having a clean workspace is the foundation of writing great code. In this guide, we will walk through setting up a professional developer workstation step by step. Step 1: Choosing and Configuring Your Editor Every good developer environment starts with a powerful code editor. Visual Studio Code (VS Code): Highly extensible and lightweight. Essential Extensions: GitLens for advanced version control tracking. Language-specific extensions (like the official Go and Rust Analyzer extensions). Docker extension to manage containers directly from your editor sidebar. Step 2: Setting Up the Toolchain (Go & Rust) To handle modern multi-language and systems programming tasks, you ne...

How to Build Your Personal Agentic System Using Ollama: A Step-by-Step Guide

Build your own private AI assistant that can plan tasks, use tools, work with files, maintain memory, and operate under controlled guardrails using Ollama and Python. Most people use AI as a chatbot: ask a question, receive an answer, and repeat. A personal agentic system is different. It can understand a goal, break it into steps, use tools, remember useful context, and produce a useful result while running largely on your own computer. In this guide, you'll build a simple but extensible personal AI agent using Ollama and Python . By the end, it will be able to: Accept a task in natural language Plan the steps needed Read and write local files Search notes or documents Call tools safely Keep a record of what it did Improve through feedback 💡 The key idea: An agent is not just a model; it is a model plus tools, memory, planning, and gu...