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.

Strings & text

Serialize a value as JSON

Go

encoding/json marshals exported fields and returns an error when a value cannot be represented.

Fragment

Import encoding/json. The surrounding function returns error.

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

data, err := json.Marshal(struct {
    Name string `json:"name"`
}{Name: "Ada"})
if err != nil { return err }

Validate incoming JSON before using it

Go

Decoding into a struct checks supported value types, but a missing field can remain its zero value. Validate required and domain constraints separately.

Fragment

Import encoding/json, strings and errors. Unmarshal ignores unknown fields; Decoder.DisallowUnknownFields offers a stricter object policy.

main.go
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
import (
    "encoding/json"
    "errors"
    "strings"
)

var input struct { Name string `json:"name"` }
if err := json.Unmarshal(data, &input); err != nil { return err }
if strings.TrimSpace(input.Name) == "" {
    return errors.New("name is required")
}

Inspect, split and slice text

Go

TrimSpace trims edge whitespace; Fields splits on runs of whitespace. TrimSuffix removes one matching suffix. len(string) and string slicing count bytes; converting []byte to string makes text from bytes.

Fragment

Import plus independent fragments.

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

fields := strings.Fields("north west")
clean := strings.TrimSpace(" west ")
length := len(clean)

Read text and character literals

Go

Double quotes interpret escapes; backticks are raw string literals. Single quotes make a rune literal. A string stores bytes; rune is an integer code-point type, not a one-character string.

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+
name := "north"
path := `folder\file`
character := 'n'
newline := '\n'

Distinguish bytes, code points and characters

Go

len(string) counts bytes. Converting to []rune or ranging a UTF-8 string decodes rune values; indexes from range remain byte offsets. Runes are not grapheme clusters.

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+
text := "🦀"
bytes := len(text) // 4 UTF-8 bytes
points := len([]rune(text)) // 1

Append and replace text

Go

Go strings are immutable byte sequences. Concatenation creates a result; strings.Builder supports incremental construction. Do not copy a nonzero Builder. Conversion to []byte creates mutable bytes.

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+
import "strings"

var builder strings.Builder
builder.WriteString("north")
builder.WriteString(" gate")
text := builder.String()

Parse structured JSON with a named implementation

Go

encoding/json decodes into a typed destination and reports errors. Unknown fields are ignored by Unmarshal into structs unless a stricter decoder is chosen.

Fragment

Import encoding/json. Missing/zero fields still need application validation.

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

type Item struct { Name string `json:"name"` }
var item Item
if err := json.Unmarshal(data, &item); err != nil { return err }