Standard library
The standard library is a set of namespaces in the prelude, so every module sees them without an import. Each namespace holds functions, and some also hold types and constants. A function takes its subject as the first argument, so you can call it through its namespace, through . on the value, or at the end of a pipe:
let words = ["ant", "bee", "cat"]
Array.map(words, { x.toUpper() }) // ["ANT", "BEE", "CAT"]
words.map({ x.toUpper() }) // ["ANT", "BEE", "CAT"]
words | Array.map({ x.toUpper() }) // ["ANT", "BEE", "CAT"]A function marked mutates changes its first argument; every other function leaves its arguments untouched and returns a new value. In the namespaces that talk to the world outside the program, an operation that can fail returns a Result with an Error rather than panicking on bad input.
Some namespaces are types as well, such as Result, Bytes, and Mutex: the name is a type in an annotation and a namespace in an expression. Others, such as Os, Time, and Reflect, are only namespaces, reached through .. A type a namespace declares is written through it, as in Http.Response or Time.Instant, and a value of that type reaches the namespace's functions through ., as in h.writeLine(line).
Core
- Prelude: the names that stand on their own:
print,println,eprint,eprintln,panic,assert,len, the conversions, and the shared types such asEntry,Seq, andNumber. - Array: the functions of arrays, ordered and growable.
- Map: the functions of maps, hash maps that keep insertion order.
- Set: the functions of sets, hash sets that keep insertion order.
- Iter: lazy sequences, which produce their elements on demand.
- String: the functions of strings, immutable Unicode text.
- Regex: regular expressions, written as a literal or compiled from data.
- Bytes: mutable, growable sequences of bytes, for binary data.
- Html: the value of an HTML expression, an immutable list of nodes.
- Math: numeric functions and constants.
- Bits: bitwise operations and wrapping arithmetic over the integer types.
- Random: seeded generators of pseudo-random numbers.
- Time: instants, dates, clock times, zones, and durations.
- Record: the functions of record values.
Errors
- Result: the outcome of an operation that can fail,
Okwith a value orErrwith an error. - Error: the error the standard library returns in an
Err, and wrapping it with context. - Panic: catching a panic as a value, and raising it again.
Concurrency
- Channel: carries values from goroutine to goroutine.
- Context: bounds a wait with a deadline or ends it with a cancellation.
- Parallel: runs work on goroutines and waits for all of it.
- WaitGroup: waits until a group of goroutines has finished.
- Mutex: a mutual-exclusion lock.
- RwMutex: a lock held by many readers or one writer.
- Once: runs a function exactly once, however many goroutines ask.
- Cond: a condition variable that goroutines wait on until another signals.
- Atomic: a cell holding one value, read and written atomically.
Files and the system
- File: the file system, open files, and the standard streams.
- Path: builds and takes apart paths as text, without touching the disk.
- Os: the process: its arguments, environment, standard streams, exit, and interrupts.
- Cmd: runs other programs, alone or in a pipeline.
Network
- Http: an HTTP client and server.
- Net: TCP listeners and connections.
- Url: takes URLs and query strings apart and puts them back together.
Data formats
- Json: converts between JSON text and values.
- Csv: converts between CSV text and rows of fields.
- Encoding: converts bytes to and from base64 and hex text.
- Crypto: cryptographic hashes, HMACs, a constant-time comparison, and secure randomness.
Tooling
- Test: what a test body needs beyond
assert, forant test. - Reflect: types as values, taken apart and used to read and build values.
Topic guides
Each guide shows how the namespaces of one area work together and when to pick which.
- Collections: arrays, maps, sets, and sequences.
- Working with text: strings, regular expressions, and bytes.
- Numbers and math: the number types, conversions, and
MathandBits. - Dates and times: instants, wall-clock times, zones, and durations.
- Errors:
Resultfor expected failure, panics for bugs. - Concurrency: goroutines, channels, contexts, and locks.
- Files and paths: reading, writing, and walking the file system.
- Processes and the OS: the program's own process and the programs it runs.
- Network: HTTP, TCP, and URLs.
- Data formats: JSON, CSV, encodings, and hashes.
- Testing: test files,
assert, andant test. - Reflection: typed decoding and encoding with
Reflect.
