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.

Types & syntax

Read common type names

Go

int is platform-sized; float64 is floating point. byte aliases uint8; rune aliases int32. []T is a slice; [N]T is a fixed-size array. make allocates a slice of the given length.

Fragment

Type declaration at file scope; value statements inside a function.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Point struct {
  X, Y int
}

p := Point{X: 2, Y: 5}
buffer := make([]byte, 3)

Decode punctuation

Go

& takes an address; *T names a pointer type. a[:n] is a slice excluding n. _ ignores a value. Double quotes make strings; single quotes make rune literals; most semicolons are inserted.

Fragment

Fragment inside a function.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
buffer := []byte{65, 66, 67}
part := buffer[:2]
_ = part
newline := '\n'
_ = newline

Represent a missing value

Go

nil applies to pointers, slices, maps, functions, channels and interfaces, not int/string/bool. Optional scalar values commonly use a pointer or a separate bool. An interface containing a typed nil pointer is not itself nil.

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+
var score *int // nil means no score
value := 0
score = &value // present zero

present := score != nil

Define a record with named fields

Go

struct groups typed fields; type gives the structure a name. Use a keyed literal to name field initializers. Capitalized fields are exported outside their package.

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+
type Point struct {
  X int
  Y int
}

point := Point{X: 2, Y: 5}
point.Y = 8

Unpack multiple values

Go

Go has multiple return values and assignment, but no general built-in tuple type. Each name receives one result; _ discards a result. Do not use _ to silently discard an error you must handle.

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+
func location() (string, int) {
  return "north", 24
}

name, count := location()

Name alternatives and tagged variants

Go

Named integer types plus const/iota commonly name modes. They do not restrict a value to only the listed constants. Go has no built-in algebraic data type with Rust-style payload variants.

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+
type Mode int

const (
  Read Mode = iota
  Write
)

mode := Read

Read a type parameter

Go

Type parameters use square brackets. any allows any type; constraints such as comparable or an interface restrict operations/types. Generic functions were introduced in Go 1.18.

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+
func identity[T any](value T) T {
  return value
}

count := identity[int](24)
name := identity("north")

Recognize a behavioral contract

Go

A type satisfies an interface implicitly through its method set. Value and pointer receivers affect which method sets satisfy it. There is no implements declaration.

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+
type Named interface {
  Name() string
}

type Place struct{}

func (Place) Name() string {
  return "north"
}

var item Named = Place{}

Check a type or request a conversion

Go

A type assertion checks an interface’s dynamic type. The two-result form reports failure without panicking. A type conversion such as int64(n) is separate and does not automatically check numeric range.

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+
var value any = "north"

text, ok := value.(string)
if ok {
  _ = text
}