193 Patterns from Go in Practice (2nd ed., 2025)

193 Patterns from Go in Practice (2nd ed., 2025)

The 2025 edition is structured around four parts: fundamentals, robust application practices, end-to-end web systems, and cloud/advanced topics (reflection, code generation, interop).

What This Adds to Our Book

  • Strong mid-level bridge: from language fluency to production engineering.
  • Better integration of testing, debugging, and benchmarking as one quality discipline.
  • Explicit chapter path toward microservices and external service integration.

End-to-End Service Model

HTTP edge -> business layer -> storage/external APIs -> telemetry -> deployment/runtime checks

Deep Integration Example: External Service Client with Typed Error Strategy

package external

import (
    "context"
    "encoding/json"
    "errors"
    "fmt"
    "net/http"
    "time"
)

var ErrUpstreamUnavailable = errors.New("upstream unavailable")

type Client struct {
    HTTP *http.Client
    Base string
}

type Profile struct {
    ID   string `json:"id"`
    Name string `json:"name"`
}

func NewClient(base string) Client {
    return Client{
        Base: base,
        HTTP: &http.Client{Timeout: 2 * time.Second},
    }
}

func (c Client) GetProfile(ctx context.Context, id string) (Profile, error) {
    ctx, cancel := context.WithTimeout(ctx, 1200*time.Millisecond)
    defer cancel()

    req, err := http.NewRequestWithContext(ctx, http.MethodGet, c.Base+"/profiles/"+id, nil)
    if err != nil {
        return Profile{}, err
    }

    resp, err := c.HTTP.Do(req)
    if err != nil {
        return Profile{}, fmt.Errorf("http call failed: %w", ErrUpstreamUnavailable)
    }
    defer resp.Body.Close()

    if resp.StatusCode >= 500 {
        return Profile{}, fmt.Errorf("status %d: %w", resp.StatusCode, ErrUpstreamUnavailable)
    }
    if resp.StatusCode != http.StatusOK {
        return Profile{}, fmt.Errorf("unexpected status: %d", resp.StatusCode)
    }

    var p Profile
    if err := json.NewDecoder(resp.Body).Decode(&p); err != nil {
        return Profile{}, err
    }
    return p, nil
}

Why This Matters

Production services fail most often at integration boundaries. This pattern teaches typed failure handling and timeout discipline in a way that scales across microservices.