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.

Control flow

Branch and exit early

Go

Conditions must be bool, without required parentheses. && evaluates its right side only when the left is true; || only when the left is false. Both return bool. An if can start with a short initialization statement. Go has no ternary operator; use if/else.

Fragment

Package-level function.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
func sign(n int) string {
  if n == 0 {
    return "zero"
  }

  if n > 0 {
    return "positive"
  }

  return "negative"
}

Loop over values

Go

for covers counting, condition-only and infinite loops; since Go 1.22 for i := range n counts 0..n-1, and Go 1.23+ also ranges over iterator functions. Range over arrays/slices yields index and value; maps yield key and value; channels yield received values. String range yields byte offsets and runes, not bytes. _ ignores an unwanted result.

Fragment

Fragment inside a function.

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

for _, name := range names {
  _ = name
}

for i := range 2 {
  _ = i
}

Select a case or match a pattern

Go

Go switch cases do not fall through automatically. An expressionless switch checks boolean cases. A type switch inspects an interface’s dynamic type; it is distinct from an expression switch.

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+
mode := "read"
label := "unknown"

switch mode {
case "read":
  label = "reader"
default:
  label = "other"
}

Repeat while a condition holds

Go

Go uses for for both condition-controlled and infinite loops; there is no while keyword. break/continue can target a labeled enclosing loop. return exits the function.

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+
n := 3

for n > 0 {
  n--
}

for {
  break
}

Read Go map, channel and newer range forms

Go

The range operand determines result shape. Map iteration order is unspecified; channel range ends after closure and draining. Integer ranges need Go 1.22+, iterator-function ranges Go 1.23+.

Fragment

All snippets assume the named values/imports and enclosing function. A channel must eventually close for this range to finish.

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

for key, value := range counts {
    fmt.Println(key, value)
}
for message := range messages {
    fmt.Println(message)
}
for index := range 3 {
    fmt.Println(index)
}