Examples¶
The examples/
directory ships a broad set of worked programs.
A friendly taste¶
examples/function_piping.gos walks through the |> forward-pipe
operator and the F#-style combinator surface in std::iter /
std::option (SPEC §10.4 / §10.4a). |> straightens out nested
calls so the data flow reads left-to-right; the combinators take
the data value as the last positional parameter so each call
threads naturally. (The same combinators are also methods on any
Vec or range - (1..=10).filter(|n| n % 2 == 0).sum() - so the
pipe form below is a stylistic choice, not the only spelling.)
use std::iter
use std::option
fn double(x: i64) -> i64 { x * 2 }
fn add(a: i64, b: i64) -> i64 { a + b }
fn clamp(lo: i64, hi: i64, x: i64) -> i64 {
if x < lo { lo } else if x > hi { hi } else { x }
}
fn main() {
let n = 3 |> double |> add(10) |> clamp(0, 100)
println!("arithmetic: {n}")
let total = iter::range_inclusive(1, 10)
|> iter::filter(|n: i64| n % 2 == 0)
|> iter::sum_by(|n: i64| n * n)
println!("sum of even squares: {total}")
let xs = [1, 3, 5, 9, 14, 21]
let first_big = xs
|> iter::find(|n: i64| n > 10)
|> option::unwrap_or(-1)
println!("first > 10 (or -1): {first_big}")
}
Running today¶
hello_world.gos- one-liner that prints viaprintln. Runs undergos run.function_piping.gos- tour of the|>forward-pipe operator plus thestd::iter/std::optioncombinator surface (filter,sum_by,find,option::unwrap_or, …). Runs undergos run(bytecode VM + Cranelift JIT),gos build(LLVM-O0), andgos build --release(LLVM-O3); the tier_parity test confirms identical output across all three.generic_struct.gos- three generic struct shapes:Pair<A, B>(two independent parameters),SameType<T>(one parameter shared by both fields, enabling field arithmetic), andTriple<A, B, C>(three parameters). Each construction site is a separate monomorphisation; parameters are inferred from the field values at the call site. Runs undergos runandgos build.trait_bounds.gos- generic functions with trait bounds dispatched statically. Onereport<T: Shape>(s: &T)serves every type that implementsShape; the bound is enforced at compile time and each instantiation monomorphises to a direct call. Identical output undergos run, the Cranelift JIT, andgos build.record_update.gos- functional record update (Config { ..base, port: 443 }): build a new struct from an existing one, overriding only the changed fields, with the base still usable afterward. Runs undergos runandgos build.go_spawn.gos- goroutine fan-out with no channels. Every construct lowers through native codegen, sogos buildproduces a working binary.concurrency.gos- goroutines plus a(Sender, Receiver)channel, producer / consumer shape. Runs undergos run(bytecode VM) andgos build(native), with channel operations lowered natively on every tier.line_count.gos- walks a directory viafs::read_dir, counts plain-text lines per file, fans out through a channel. Uses goroutines andselect.web_server.gos- HTTP/1.1 echo server mirroring FastAPI's/echohandler. Accepts any method, returns method / path / query / body as JSON. Runs undergos run;curl http://localhost:8080/echo?name=janeexercises it.
More in the tree¶
The examples/
directory ships many more worked programs - collections and data
structures, error handling, file and directory I/O, encoding
(JSON / YAML / TOML / base64 / hex), crypto hashing, regular
expressions, an HTTP client and server, compression, CLI argument
parsing, and a full multi-file project under examples/projects/.
Each one passes gos check; see examples/README.md for the index.