Go Examples

Page 7 of 8

Browse Go examples

goSnippet

Sync.Map LoadOrStore And Range

Guard a shared cache with sync.Map: LoadOrStore inserts atomically when absent, Load reads with comma-ok, and Range walks entries without blocking writers.

goSnippet

Ticker Periodic Tasks With Stop

Drive periodic maintenance with time.NewTicker, consume ticks in a select loop alongside a stop channel, and always Stop the ticker so timers do not leak.

goSnippet

Sync Cond Wait And Signal

Coordinate goroutines with sync.Cond: waiters park in a loop until a producer changes the guarded condition and calls Signal.

goSnippet

Sync Once Lazy Singleton Init

Initialize expensive resources exactly once with sync.Once, keeping first-call latency on the caller and all later calls lock-free after the gate.

goSnippet

Fixed Worker Pool

Process a stream of jobs with a fixed number of goroutines, bounded channels and graceful completion.

goSnippet

Gin JWT Claims Middleware

Extract and verify JWT claims in Gin middleware, checking issuer and role before handlers, with claims injected into context.