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

TypeScript

path.join constructs a platform path. It does not by itself establish that untrusted input stays inside an allowed directory.

Fragment

Node program. Resolve/validate any containment policy separately from joining path segments.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import path from "node:path";
const file = path.join(directory, "settings.json");

List the immediate entries in a directory

TypeScript

withFileTypes returns Dirent records for immediate children. Directory listings are not automatically sorted.

Fragment

Place await inside an async function in a CommonJS project. A recursive walk is a separate policy, including how to handle links/errors.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { readdir } from "node:fs/promises";
const entries = await readdir(directory, { withFileTypes: true });
for (const entry of entries) {
  console.log(entry.name, entry.isDirectory());
}

Create a directory and missing parents

TypeScript

recursive creates missing parent directories and tolerates the destination already being a directory.

Fragment

Use inside an async function; permissions and conflicting existing files can still make the operation fail.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { mkdir } from "node:fs/promises";
await mkdir(directory, { recursive: true });

Copy a chosen file with an overwrite policy

TypeScript

copyFile copies bytes and overwrites the destination by default. COPYFILE_EXCL requests failure when the destination already exists.

Fragment

Node API; use inside an async function. Copying is not an atomic replacement protocol.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { copyFile, constants } from "node:fs/promises";
await copyFile(source, destination, constants.COPYFILE_EXCL);

Read file text with explicit error and encoding behavior

TypeScript

Node fs/promises reads a file asynchronously. Naming utf8 returns decoded text; omitting it returns a Buffer.

Fragment

Top-level await requires an appropriate module context. Node’s UTF-8 decoding replaces malformed sequences; validate separately when rejection is required.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { readFile } from "node:fs/promises";
const text = await readFile("notes.txt", "utf8");

Write or replace file contents

TypeScript

writeFile creates or replaces a file by default. Choose an explicit append flag when appending is the contract.

Fragment

A successful write is not a promise of crash-safe persistence or an atomic replacement protocol.

main.ts
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import { writeFile } from "node:fs/promises";
await writeFile("output.txt", "ready\n", "utf8");