Go (Golang) Tutorial 0/45 lessons ~6 min read Lesson 32

    REST API Development

    Building a REST API in Go combines net/http routing, JSON encoding, validation, and layered architecture (handler → service → repository).

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Building a REST API in Go combines net/http routing, JSON encoding, validation, and layered architecture (handler → service → repository). Go 1.22's enhanced ServeMux simplifies routing without external frameworks, though chi, gin, and echo remain popular for larger projects.

    REST conventions — proper HTTP methods, status codes, resource naming, and error response format — matter as much as syntax. Interviewers ask you to design CRUD endpoints, handle validation errors as 400, and return consistent JSON error envelopes.

    This lesson implements a complete Product API with create, read, update, delete, and structured error responses using only the standard library plus idiomatic patterns.

    The story

    An internal API gateway at a fintech company exposes CRUD endpoints for merchant accounts: GET /merchants/{id}, POST /merchants, PATCH /merchants/{id}. Handlers parse path parameters, validate JSON bodies, map domain errors to HTTP status codes, and return consistent error envelopes — the contract frontends and B2B integrators depend on.

    REST in Go typically layers chi, gorilla/mux, or stdlib ServeMux (1.22+) with middleware for auth, request IDs, and Prometheus metrics.

    Understanding the topic

    Key concepts

    • Resources as nouns: /products, /products/{id} — verbs via HTTP methods.
    • GET read, POST create, PUT/PATCH update, DELETE remove.
    • Status: 200 OK, 201 Created, 400 Bad Request, 404 Not Found, 500 Internal.
    • Handler parses request → calls service → writes JSON response.
    • Consistent error JSON: {"error":"message","code":"VALIDATION"}.
    • Middleware chain: recovery, logging, auth, CORS.
    Visual overview
    Client → Server
    HTTP Request
    Server → Handler
    ServeHTTP
    Handler → Service
    Business Logic
    Service → DB
    Query
    DB → Handler
    Data
    Handler → Client
    JSON Response

    Step-by-step explanation

    1. Router maps method+path to handler function.
    2. Handler decodes JSON body into request DTO.
    3. Service validates business rules, calls repository.
    4. Repository persists to database or in-memory store.
    5. Handler encodes response DTO or error with correct status.
    6. Integration tests spin httptest.Server against handlers.

    Practical code example

    REST Product API with handler, service layer, and JSON error responses:

    go
    package main
    import (
    "encoding/json"
    "errors"
    "net/http"
    "sync"
    )
    type Product struct {
    ID string `json:"id"`
    Name string `json:"name"`
    Price float64 `json:"price"`
    }
    type ProductStore struct {
    mu sync.RWMutex
    products map[string]Product
    }
    func (s *ProductStore) Get(id string) (Product, error) {
    s.mu.RLock()
    defer s.mu.RUnlock()
    p, ok := s.products[id]
    if !ok {
    return Product{}, errors.New("not found")
    }
    return p, nil
    }
    func (s *ProductStore) Save(p Product) {
    s.mu.Lock()
    defer s.mu.Unlock()
    s.products[p.ID] = p
    }
    func writeJSON(w http.ResponseWriter, status int, v any) {
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(status)
    _ = json.NewEncoder(w).Encode(v)
    }
    func productHandler(store *ProductStore) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
    id := r.PathValue("id")
    switch r.Method {
    case http.MethodGet:
    p, err := store.Get(id)
    if err != nil {
    writeJSON(w, http.StatusNotFound, map[string]string{"error": "product not found"})
    return
    }
    writeJSON(w, http.StatusOK, p)
    case http.MethodPut:
    var p Product
    if err := json.NewDecoder(r.Body).Decode(&p); err != nil {
    writeJSON(w, http.StatusBadRequest, map[string]string{"error": "invalid json"})
    return
    }
    p.ID = id
    store.Save(p)
    writeJSON(w, http.StatusOK, p)
    default:
    writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "method not allowed"})
    }
    }
    }
    func main() {
    store := &ProductStore{products: make(map[string]Product)}
    mux := http.NewServeMux()
    mux.HandleFunc("GET /products/{id}", productHandler(store))
    mux.HandleFunc("PUT /products/{id}", productHandler(store))
    http.ListenAndServe(":8080", mux)
    }

    Line-by-line code explanation

    • ID string `json:"id"` is a struct field with a tag. The tag tells encoding/json to use the lowercase key id in JSON; the Name and Price fields have similar tags.
    • mu sync.RWMutex sits next to the products map in ProductStore. A map is not safe for concurrent use, and every HTTP request runs in its own goroutine, so the mutex guards access to it.
    • p, ok := s.products[id] looks up the product while holding a read lock (RLock, released by defer). Many readers can hold the read lock at the same time. If the id is missing, Get returns an empty Product and an error.
    • s.mu.Lock() in Save takes the exclusive write lock, so no one can read or write the map while the new product is stored under its ID.
    • w.WriteHeader(status) sends the status code. In writeJSON the Content-Type header is set before it, because headers can no longer be changed once WriteHeader has been called. The body is then written by a JSON encoder.
    • id := r.PathValue("id") reads the {id} part of the matched URL, for example the 42 in /products/42. This works because of the {id} wildcard in the routes registered in main.
    • switch r.Method { picks the behaviour by HTTP method: GET reads a product (404 if missing) and PUT saves one. Any other method that reaches the handler, such as HEAD, which the GET pattern also matches, gets a 405 response from the default case.
    • json.NewDecoder(r.Body).Decode(&p) reads the request body JSON into a Product. If that fails the handler answers 400; otherwise it overwrites the ID with the one from the URL, saves the product and returns it with 200.
    • mux.HandleFunc("PUT /products/{id}", productHandler(store)) registers the same handler for PUT, just as the line above does for GET. Both routes share one ProductStore created in main, and the server then listens on port 8080.

    Key takeaway: The handler reads the id with r.PathValue, and the store protects its map with a read/write mutex so concurrent requests are safe. writeJSON sets the header, status and JSON body in one place, so every response is built the same way.

    Real-world use

    Where you'll use this in production

    • E-commerce product catalog and order APIs.
    • SaaS multi-tenant REST backends.
    • Mobile app BFF (backend-for-frontend) services.
    • Public developer APIs with rate limiting and API keys.

    Best practices

    • Use correct HTTP method and status for each operation.
    • Validate input; return 400 with clear error message.
    • Separate handler, service, repository layers.
    • Version APIs: /v1/products for backward compatibility.
    • Document with OpenAPI/Swagger for consumers.
    • Use httptest for handler integration tests.

    Common mistakes

    • 200 for everything including errors.
    • Business logic directly in handler — untestable.
    • Inconsistent error JSON shape across endpoints.
    • No request body size limit — DoS via large payload.
    • PUT vs PATCH confusion — PUT replaces, PATCH partial update.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    5 questions
    1BeginnerQuestionREST vs RPC?+

    Answer

    REST resource-oriented with HTTP verbs; RPC action-oriented (gRPC POST /doThing).
    2BeginnerQuestion201 vs 200?+

    Answer

    201 Created for new resource with Location header; 200 for successful read/update.
    3IntermediateQuestionStructure Go REST project?+

    Answer

    cmd/api/main, internal/handler, internal/service, internal/repo, internal/domain.
    4IntermediateQuestionValidate request body?+

    Answer

    Decode JSON, run validator (go-playground/validator), return 400 with field errors.
    5AdvancedQuestionDesign idempotent POST for payments.+

    Answer

    Idempotency-Key header; store key→response; return cached response on duplicate.

    Summary

    REST maps resources to URLs and HTTP methods. Layer handlers, services, and repositories for testability. Return correct status codes and consistent JSON errors. Go 1.22 ServeMux simplifies routing with path parameters. Next lesson: PostgreSQL/MySQL with database/sql.

    Ready to mark this lesson complete?Track your journey across the entire course.