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.

Print & read input

Print a value or a line

Go

Println appends a newline; Printf applies formatting without adding one unless the format contains it.

Fragment

Import fmt; place calls inside a function.

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

fmt.Println("Hello")
fmt.Println("count:", 3)
fmt.Printf("count=%d\n", 3)

Write diagnostics to stderr

Go

Fprintln can target stderr. Return errors to an owner that chooses the process status.

Fragment

Import fmt and os. os.Exit skips deferred functions; do cleanup before using it.

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

fmt.Fprintln(os.Stderr, "Could not load settings")

Log a message with useful context

Go

The standard slog package supports levels and structured key/value attributes.

Fragment

Go 1.21+. Import log/slog. Configure a handler once when you want JSON or a custom destination.

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

slog.Info("item saved", "item_id", 24, "elapsed_ms", 8)

Print or build text

Go

Println prints with a newline; Printf uses a format without adding one. Sprintf returns the formatted string. strconv.Quote returns a quoted, escaped Go string literal.

Fragment

Imports plus local fragments.

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

text := fmt.Sprintf("item %d", 24)
fmt.Println(strconv.Quote(text))

Recognize formatting controls

Go

Format verbs choose representation: %d integer, %s string, %v default. Width is a minimum; precision controls digits after the decimal for %f. A 0 flag requests zero padding.

Fragment

Import plus independent fragments.

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

price := fmt.Sprintf("%.1f", 6.2)
tag := fmt.Sprintf("%4s", "x")

Read a line

Go

ReadString reads through and retains the delimiter. It returns text plus an error; EOF can accompany final text. Reuse the buffered reader across reads.

Fragment

Import plus read fragment; no error policy assumed.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import (
  "bufio"
  "os"
)

reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
// Inspect both line and err.

Convert text to a number

Go

Atoi returns (int, error); ParseFloat(text, 64) returns (float64, error). They report invalid syntax/range separately from the value. Neither trims surrounding whitespace.

Fragment

Import plus independent conversion fragments.

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

n, err := strconv.Atoi("24")
amount, floatErr := strconv.ParseFloat("6.2", 64)
// Inspect errors before using values.

Recognize raw input scaffolding

Go

Read fills a byte slice and returns (n, error). Only buffer[:n] contains this read’s bytes; process n before handling an accompanying error. Names such as LinePrinter are application-defined, not standard I/O types.

Fragment

Import plus single-read fragment.

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

buffer := make([]byte, 8)
n, err := os.Stdin.Read(buffer)
// Inspect buffer[:n] and err.