Practical developer resources

Code answers, ready to copy.

Search 8,765 focused examples, tutorials, API docs, cases and templates across 16 resource types. Every snippet is verified for accuracy before it ships.

Featured scenarios:form validationfile uploadauth jwtdata fetchingcli toolingresiliencedata integritysecurity boundaries

goSwap Immutable Snapshots With atomic.Pointer
package main

import (
	"fmt"
	"sync/atomic"
)

type RouteTable struct {
	Version int
	Primary string
}

func main() {
	var current atomic.Pointer[RouteTable]
	current.Store(&RouteTable{Version: 1, Primary: "us-east"})

	// Reader holds an immutable snapshot before the swap
	pinned := current.Load()
	fmt.Printf("pinned snapshot: v%d -> %s\n", pinned.Version, pinned.Primary)

	// Writer swaps in a brand-new immutable struct without mutating v1
	prev := current.Swap(&RouteTable{Version: 2, Primary: "eu-west"})
	fmt.Printf("swapped out: v%d -> %s\n", prev.Version, prev.Primary)

	latest := current.Load()
	fmt.Printf("latest snapshot: v%d -> %s\n", latest.Version, latest.Primary)
	fmt.Printf("pinned unchanged: v%d -> %s\n", pinned.Version, pinned.Primary)
	fmt.Println("same pointer:", pinned == prev)
}

Browse by resource type

All resources →

Recently verified

View all →

Interactive Developer Tools

All 200 tools →

Browse by scenario