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.
Async & concurrency TypeScript promises, Go goroutines/context, Rust futures with Tokio where named, and C++ threads/futures. Runtime and cancellation rules differ; optional setup is identified in each entry.
Go
go starts a goroutine. Starting it does not give the caller a built-in result handle.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy
Go
Receive a value through an explicit channel, or call a synchronous function in the current goroutine.
Fragment
A channel receive blocks that goroutine until a value/closure is available.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy
Retain ownership of a background task # Go
Track completion explicitly with WaitGroup; Go 1.25+ wg.Go starts the goroutine and does the Add/Done bookkeeping. It does not collect return values or errors.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import "sync"
var wg sync.WaitGroup
wg.Go(func() {
work()
})
wg.Wait()
Sequential awaits versus overlapping work # Go
A normal call finishes before the next statement. Use goroutines plus explicit joining/communication to overlap work.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy go loadLeft(leftResults)
go loadRight(rightResults)
a, b := <-leftResults, <-rightResults
Let other work progress # Go
The runtime schedules goroutines. runtime.Gosched yields execution, but is not synchronization or a completion guarantee.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import "runtime"
runtime.Gosched()
Wait for all successful results # Go
WaitGroup waits for completion, not success. Use an explicit result/error collection, or a named task-group library.
Fragment
Never call Wait before the necessary Add calls can be observed.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy wg.Wait()
// Inspect the separately collected results/errors.
Collect successes and failures # Go
Collect a result struct from each worker; keep the expected count or close the channel after all producers finish.
Fragment
The producer owner must eventually close results for range to terminate.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy type Result struct {
Value int
Err error
}
for result := range results {
record(result)
}
Propagate async/task failures # Go
Return an error value to the caller. A goroutine must communicate its error explicitly.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import "fmt"
value, err := work()
if err != nil {
return fmt.Errorf("work: %w", err)
}
use(value)
Clean up after asynchronous work # Go
defer schedules cleanup when the surrounding function returns, including ordinary early returns.
Fragment
defer inside a long loop waits until the containing function returns.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy resource, err := acquire()
if err != nil { return err }
defer resource.Close()
return use(resource)
Go
Canceling a context closes Done. Work must select on it or call APIs that accept the context.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import "context"
ctx, cancel := context.WithCancel(parent)
defer cancel()
go worker(ctx)
cancel()
Bound the duration of work # Go
WithTimeout derives an earlier cancellation boundary; call cancel to release its resources.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import (
"context"
"time"
)
ctx, cancel := context.WithTimeout(parent, 3*time.Second)
defer cancel()
return work(ctx)
Wait for the first completion # Go
select chooses one ready communication case; if multiple cases are ready, selection is pseudo-random.
Fragment
Cancel or otherwise finish losing work explicitly.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy select {
case value := <-results:
use(value)
case <-ctx.Done():
return ctx.Err()
}
Delay work with the runtime’s timer model # Go
A timer can be selected with cancellation. Stop a timer you own when it is no longer needed.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import "time"
timer := time.NewTimer(100 * time.Millisecond)
defer timer.Stop()
select {
case <-timer.C:
case <-ctx.Done():
return ctx.Err()
}
Send a value through a channel # Go
An unbuffered send waits for a receiver. A buffered channel allows sends until its capacity is full.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy jobs := make(chan Job, 4)
jobs <- job
received := <-jobs
Distinguish channel closure from a value # Go
A two-value receive distinguishes a sent zero value from closure. Sending on a closed channel panics; receivers should not close a shared producer channel.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy value, ok := <-results
if !ok {
// Channel closed and buffered values drained.
}
Recognize a backpressure boundary # Go
A bounded channel makes the producer wait when its buffer is full. Add a context case when a send must be cancelable.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy select {
case jobs <- job:
case <-ctx.Done():
return ctx.Err()
}
Consume a stream incrementally # Go
range receives until a channel is closed and drained. It does not by itself observe context cancellation.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy for chunk := range chunks {
process(chunk)
}
Limit concurrent operations # Go
Acquire a semaphore token before launching each goroutine; release it on completion.
Fragment
This fragment omits error collection and cancelable acquisition; add them to the owning workflow.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import "sync"
slots := make(chan struct{}, 4)
var wg sync.WaitGroup
for _, item := range items {
slots <- struct{}{}
wg.Go(func() { // Go 1.25+; each iteration has its own item (Go 1.22+)
defer func() { <-slots }()
process(item)
})
}
wg.Wait()
Protect a shared invariant # Go
A mutex protects the complete invariant when every participant follows the same locking rule.
Fragment
A data-race-free program can still have logical races if the critical section is too small.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy mu.Lock()
count++
mu.Unlock()
Move blocking or CPU-heavy work appropriately # Go
Goroutines can execute CPU work across runtime-managed threads, subject to GOMAXPROCS and available resources.
Fragment
A goroutine per input can still overload memory/CPU; bound work.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy
Separate shared ownership from synchronization # Go
Channels, mutexes and atomics provide synchronization with defined memory-model relations. Starting goroutines alone does not make unsynchronized access safe.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import "sync/atomic"
var count atomic.Int64
count.Add(1)
value := count.Load()
Keep child work inside an owned lifetime # Go
A context distributes cancellation; it does not wait for goroutines to stop. Join them separately.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy
Establish the runtime and API baseline # Go
Go’s runtime schedules goroutines; concurrency primitives come from the standard library. Typed atomic.Int64 requires Go 1.19+.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy package main
import "sync/atomic"
func main() {
var count atomic.Int64
count.Add(1)
}
Cancel cooperating siblings and observe completion # Go
errgroup is an optional golang.org/x/sync dependency, not part of sync.WaitGroup. WithContext distributes cancellation and Wait observes the group result.
Fragment
Import golang.org/x/sync/errgroup and pin a version compatible with your Go toolchain. Work must cooperate with context cancellation. SetLimit can bound active group goroutines.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy import "golang.org/x/sync/errgroup"
group, ctx := errgroup.WithContext(parent)
for _, item := range items {
group.Go(func() error { return work(ctx, item) })
}
if err := group.Wait(); err != nil { return err }
Bound task records and consume results while launching # Go
A bounded jobs channel plus a fixed worker count limits queued/running items. A result consumer must run concurrently if workers can block sending results.
Fragment
A buffer alone does not bound retained inputs elsewhere. Avoid waiting for workers before starting the only result consumer.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy for result := range results {
consume(result)
}
// Producers/worker owner close results after all sends finish.
Account for progress when a wait is canceled # Go
Selecting ctx.Done ends this wait, not necessarily the worker. If a send/receive was selected, that channel operation happened; ownership must define what happens to the received item.
Fragment
A canceled context can race with a ready channel; either ready case may be selected. Use the two-value receive when closure matters.
main.go Follow appearance Follow appearance
✓ GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Copy select {
case item := <-jobs:
return process(ctx, item)
case <-ctx.Done():
return ctx.Err()
}