Go snippets

Keep your team's Go snippets in one base.

Go keeps the language small on purpose, which means every team accumulates the same handful of load-bearing snippets: graceful shutdown, bounded goroutines, a retry with jitter, an HTTP client with real timeouts. They're short, they're subtle, and they get re-derived — slightly wrong — in every new service. The-Snip keeps your team's canonical versions in one reviewed, searchable base, with the deploy runbooks and saved API calls beside them. Your agents read the same base over REST and MCP, so Claude Code reaches for your shutdown pattern instead of inventing one, and anything it writes back waits in review.

Go snippets worth keeping

Graceful HTTP server shutdown

srv := &http.Server{Addr: ":8080", Handler: mux}

go func() {
    if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
        log.Fatal(err)
    }
}()

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM)
<-stop

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
    log.Printf("shutdown: %v", err)
}

Finish in-flight requests before the process exits — the snippet every rolling deploy silently assumes you have. Without it, restarts drop whatever was mid-request.

Bounded parallelism with a semaphore

sem := make(chan struct{}, 8)
var wg sync.WaitGroup

for _, job := range jobs {
    wg.Add(1)
    go func(j Job) {
        defer wg.Done()
        sem <- struct{}{}
        defer func() { <-sem }()
        process(j)
    }(job)
}
wg.Wait()

Run at most eight jobs at once without importing a pool library. A buffered channel is the whole mechanism — acquire by send, release by receive.

Retry with backoff, jitter and context

func retry(ctx context.Context, attempts int, fn func() error) error {
    var err error
    for i := 0; i < attempts; i++ {
        if err = fn(); err == nil {
            return nil
        }
        d := time.Duration(1<<i)*100*time.Millisecond +
            time.Duration(rand.Intn(100))*time.Millisecond
        select {
        case <-time.After(d):
        case <-ctx.Done():
            return ctx.Err()
        }
    }
    return err
}

Backoff doubles per attempt, jitter breaks retry lockstep, and the select respects cancellation — so a caller timing out actually stops the retries.

Strict JSON decoding

func decodeStrict[T any](r io.Reader) (T, error) {
    var v T
    dec := json.NewDecoder(r)
    dec.DisallowUnknownFields()
    err := dec.Decode(&v)
    return v, err
}

Reject unknown fields at the boundary. Catches client/server drift the day it happens instead of three weeks later when the ignored field mattered.

HTTP client with sane timeouts

var client = &http.Client{
    Timeout: 10 * time.Second,
    Transport: &http.Transport{
        MaxIdleConnsPerHost: 20,
        IdleConnTimeout:     90 * time.Second,
    },
}

http.DefaultClient has no timeout — a hung upstream hangs you. Every service repo ends up needing this exact literal; keep one canonical copy instead.

Questions, answered.

Does The-Snip highlight Go correctly?

Yes — Go and 35+ other languages, highlighted server-side with the same grammars VS Code uses. Channels, generics and struct tags all tokenize correctly, in light and dark.

Can Claude Code or Cursor pull these snippets while coding?

Yes. Connect the hosted MCP server (or the REST API) with a workspace key — reads like search_base are free on every plan; agent writes need the one Pro & Team plan, $8/user/month. The agent calls search_base before writing shared code and reuses your canonical version instead of re-deriving it.

How do we keep exactly one canonical version of a helper?

The review workflow. New and updated items land in review; a reviewer approves, edits or rejects. Search surfaces the approved canon, so "which retry do we use?" has one answer for both humans and agents.

Can the curl for a service live next to its Go client?

Yes — saved API calls store method, URL, headers and body, and copy out as a ready-made curl. Link the snippet, the request and the runbook together with [[wikilinks]] so on-call finds all three at once.

What does it cost?

Free covers 25 items with no card, plus read-only agent access over REST and MCP. The single paid plan — Pro & Team, $8/user/month or $80/user/year — adds unlimited items, export, agent writes (create and update), team workspaces and review.

Start your base — free.

Free: 25 items, no card. Pro & Team: $8/user/mo — unlimited items, REST API, review workflow, and the hosted MCP server.