Go Examples

Page 5 of 8

Browse Go examples

goSnippet

Channel Fastest Response Wins Race

Launch several workers that race to produce a value, then take the first arrival from a shared channel and cancel nothing because losers simply finish unused.

goSnippet

Compose Interfaces By Embedding

Build a composite interface by embedding io.Reader and io.Closer plus your own method, so callers depend on capability sets instead of concrete types.

goSnippet

Read And Write Whole Files

Use os.ReadFile and os.WriteFile for whole-file IO with explicit permissions, and classify read failures with errors.Is instead of panicking.

goSnippet

Format Numbers With Strconv

Render integers in any base, floats with fixed precision, and quoted strings with strconv.Format* and Append* helpers for allocation-free building.

goSnippet

Exact Ratios With big.Rat

Represent rates as big.Rat rational numbers so one-third stays exact in division, comparison, and re-float, instead of drifting through float64.

goSnippet

Defer In Loops Delays Cleanup

See why defer inside a loop holds every resource until the function returns, and how extracting the iteration body closes each file on schedule.

goSnippet

Select Files With Glob Patterns

Match file names against shell-style patterns with filepath.Glob and fs.Glob, getting sorted results and an empty slice rather than an error on no match.

goSnippet

Float NaN And Inf Guards

Sqrt of negatives yields NaN that fails every comparison, overflow yields Inf; IsNaN and IsInf guards keep NaN out of aggregated sums before it spreads.