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.

Functions

Define and call a function

Go

Parameters use name type; the result type follows ). Multiple results are allowed. Omit the result type when nothing is returned.

Fragment

Function declaration, then a call fragment inside main.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
func double(n int) int {
  return n * 2
}

result := double(6)

Find the entry point

Go

An executable uses package main with a parameterless main function. Top-level function declarations do not run just because they are declared.

Fragment

Complete main.go.

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

import "fmt"

func main() {
  fmt.Println("north")
}

Recognize custom types and methods

Go

struct declares fields; the receiver before the method name binds the method to a type. A *Counter receiver can update the original. LinePrinter, Push and Finish are exercise-defined.

Fragment

Type and method declarations at file scope; value statements inside a function.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
type Counter struct {
  value int
}

func (c *Counter) Add() {
  c.value++
}

var counter Counter
counter.Add()

Construct a custom value

Go

Go has no class constructor syntax. A struct literal initializes fields; a function named New or NewType is a convention, not a keyword. It may return a value or pointer. The built-in new(T) separately returns a pointer to a zero-valued T.

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 Token struct {
  Value int
}

func NewToken(value int) Token {
  return Token{Value: value}
}

item := NewToken(24)

Pass a function or capture a value

Go

A function literal can close over surrounding variables. It is a value with a function type. Captured variables can outlive the call that created the closure; capture is not necessarily a value snapshot.

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+
factor := 3
scale := func(n int) int {
  return n * factor
}

result := scale(2)

Recognize defaults and variable arguments

Go

Go has no default/optional parameters in ordinary function signatures. A final ...T parameter receives a slice. values... expands a slice in a variadic call; it does not create function overloading.

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 sum(values ...int) int {
  total := 0
  for _, value := range values {
    total += value
  }
  return total
}

values := []int{2, 5}
total := sum(values...)