Go Examples

Page 4 of 8

Browse Go examples

goSnippet

Parse Go Source With go parser

Parse Go source text into a syntax tree with go/parser, then walk it with ast.Inspect to report every function declaration and its parameter count.

goSnippet

Filter Slices In Place

Filter a slice into its own backing array with s[:0] and append, then prove the aliasing by reading the stale tail beyond the truncated length.

goSnippet

TrimLeft Cutset Versus Prefix

Strip leading characters correctly: strings.TrimLeft removes any leading cutset runes, TrimPrefix removes one exact prefix, and confusing them corrupts data.

goSnippet

Adler32 Rolling Checksum

Feed bytes through hash/adler32 incrementally and compare the running digest with the one-shot Checksum helper to prove both paths agree.

goSnippet

Atomic Bool Swap Flag

Use atomic.Bool Swap and CompareAndSwap to flip a shared flag across goroutines without a mutex, watching each returned value.

goSnippet

Benchmarks With ReportAllocs

Measure throughput and allocations with testing.B, ReportAllocs, and sub-benchmarks so optimization claims rest on numbers you can reproduce.

goSnippet

FNV Hashing For Shard Keys

Hash keys with hash/fnv for deterministic shard assignment, and reuse one hasher via Reset to avoid allocating on every call.

goSnippet

HKDF Key Derivation

Derive separate encryption keys from one shared secret with HKDF extract, expand, and label binding instead of reusing raw keys.

goSnippet

Strings Lines Iterator

Iterate over a string with strings.Lines and see how each yielded value keeps its line terminator, including CRLF input and an empty line.