Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Collections

Create an indexed collection

Go

[N]T is an array with length in its type; []T is a slice over an array. append returns the resulting slice and may allocate new storage, so keep its return value. len and cap are distinct.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
fixed := [3]int{2, 5, 8}
values := []int{2, 5}
values = append(values, 8)

length := len(values)
capacity := cap(values)

Look up a value by key

Go

Map keys must be comparable. Lookup returns the value type’s zero value when absent; the second result reports presence. Reading a nil map is allowed; writing one panics. Iteration order is unspecified.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
scores := map[string]int{"north": 24}
scores["west"] = 8

score, found := scores["north"]
delete(scores, "west")

Keep unique values

Go

Go has no built-in set type. A map[T]struct{} commonly represents membership for comparable keys. Use comma-ok lookup; a map[T]bool can distinguish a stored false from a missing key only with that second result.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
labels := map[string]struct{}{}
labels["north"] = struct{}{}

_, found := labels["north"]
delete(labels, "north")

Select a range or borrow a view

Go

a[low:high] shares backing storage and excludes high. Element mutations are visible through aliases. append may reuse or replace storage depending on capacity. copy copies elements into an existing destination.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
values := []int{2, 5, 8}
part := values[1:3]
part[0] = 9 // values[1] is now 9

separate := make([]int, len(part))
copy(separate, part)

Sort with the right comparator shape

Go

slices.Sort sorts in place. SortFunc takes a negative/zero/positive comparator; SortStableFunc also preserves the order of equivalent elements. The slices package requires Go 1.21 or newer.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import "slices"

values := []int{12, 2, 5}
slices.Sort(values)

Map, filter and accumulate

Go

Slices do not have built-in map/filter/reduce methods. A direct range loop commonly expresses this work. Custom generic helpers are possible, but are not methods supplied by []T itself.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
values := []int{2, 5, 8}
total := 0

for _, n := range values {
  if n > 3 {
    total += n * 2
  }
}