Examples

Page 20 of 50

Browse code examples

sqlSnippet

SELECT FOR SHARE Read Locking

FOR SHARE takes a row lock that blocks writers but permits other readers, unlike the plain snapshot read that lets a concurrent UPDATE slip between checks.

sqlSnippet

Weighted Average Per Group

Compute a weighted average as SUM(value * weight) over SUM(weight), because averaging already-averaged group values weights small samples the same as big ones.

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.