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.

Files & paths

Join paths using the platform’s path API

Go

filepath.Join uses native path separators and lexical cleaning.

Fragment

Import path/filepath. A joined path does not provide a security boundary.

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

file := filepath.Join(directory, "settings.json")

List the immediate entries in a directory

Go

os.ReadDir returns immediate directory entries and an error. Entries are sorted by filename.

Fragment

Import os and fmt. The enclosing function returns error.

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

entries, err := os.ReadDir(directory)
if err != nil { return err }
for _, entry := range entries {
    fmt.Println(entry.Name(), entry.IsDir())
}

Create a directory and missing parents

Go

MkdirAll creates missing parents and reports failures. The requested mode is affected by the process umask.

Fragment

Import os. Existing directories keep their existing permissions.

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

if err := os.MkdirAll(directory, 0o755); err != nil {
    return err
}

Copy a chosen file with an overwrite policy

Go

Go has no os.CopyFile convenience function. This small-file recipe explicitly reads bytes and creates a new destination exclusively.

Fragment

Import os. Holds the source in memory; for large files stream with io.Copy and own both file lifetimes. A failed write can leave a partial destination.

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

data, err := os.ReadFile(source)
if err != nil { return err }
file, err := os.OpenFile(destination, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600)
if err != nil { return err }
if _, err := file.Write(data); err != nil {
    file.Close()
    return err
}
return file.Close()

Read file text with explicit error and encoding behavior

Go

os.ReadFile returns bytes and an error. Converting []byte to string does not validate UTF-8.

Fragment

Import os; place usage inside an error-returning function. Use unicode/utf8.Valid when validation is required.

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

data, err := os.ReadFile("notes.txt")
if err != nil { return err }
text := string(data)

Write or replace file contents

Go

os.WriteFile creates/truncates and writes bytes. The permission argument applies when creating a new file, subject to the process umask.

Fragment

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

if err := os.WriteFile("output.txt", []byte("ready\n"), 0o600); err != nil {
    return err
}