More in Go & Rust — page 8

TCP Listeners: Go vs Rust
Go spins up TCP listeners and handles connections with lightweight goroutines—1 million costs ~500MB—but GC pauses introduce 2-5ms latency. Rust trades boilerplate for zero-cost safety and consistent sub-100µs response times without garbage collection.
go vet: Catch Bugs Compilers Allow
go vet catches suspicious constructs the compiler ignores, like Printf argument mismatches. Run it in CI to spot concurrency and formatting bugs early. It relies on heuristics, so a clean report does not guarantee correctness and false positives can occur.
CSP: Model Concurrency with Message Passing
CSP treats concurrency as isolated processes talking through channels, not threads fighting over shared memory. It shaped Go, Erlang, and occam. Engineers often retrofit shared-state patterns into channel-based code and reintroduce race conditions.
go.mod: Root of Go Module Identity
A go.mod file anchors a Go module, declaring its canonical path and dependencies to turn a directory into a versioned unit. Every project needs one at its root, and the path dictates how others import your packages.
The Newtype Pattern: Type Safety for Primitives
Wrap a primitive type in a new struct to give it a unique, compile-time identity. A `Miles(f64)` is different from a `Kilometers(f64)`. Use it to prevent mixing up IDs or units. The footgun: you must explicitly implement or delegate methods for the new type.
Rust's Serde: Taming JSON with Types
Serde JSON translates between human-readable JSON text and native Rust structs, acting as a bilingual interpreter for your data. Use it for web APIs or config files.
Rust's TcpStream: Your Handle to a Network Connection
A `TcpStream` is Rust's handle to a network connection, closing automatically when it goes out of scope. Use it to talk to servers. The footgun: `connect()` can block forever; always prefer `connect_timeout()` in production to avoid hanging.
Rust Enums and Pattern Matching: Type-Safe Alternatives
Rust enums define a type that can be one of several variants, each holding its own data. They're used to model states like `Loading`/`Success`/`Error` or handle optional values with `Option<T>`.
Rust's Fearless Concurrency: Catch Bugs Before They Ship
Rust's "fearless concurrency" uses the ownership and type system to turn data races into compile-time errors. This allows you to safely use threads, message passing, or shared state without runtime surprises. The footgun is assuming this prevents all bugs.
Rust Lifetimes: Preventing Dangling References
Lifetimes are Rust's compile-time guarantee that a reference never outlives the data it points to. The borrow checker uses them to prevent dangling pointers, a common source of bugs.
Daemonizing Go/Rust Apps: Let the OS Do It
Daemonizing an app means running it as a background service, detached from your terminal. This is essential for web servers or job processors. The common footgun is writing custom daemon logic instead of using a system service manager like systemd.
The FromRequest Trait: Consuming Request Bodies in Axum
Axum's `FromRequest` trait defines how to create a type by consuming an HTTP request body. It's the core of extractors like `Json<T>` that deserialize POST data. The footgun: you can only use one `FromRequest` extractor per handler, as it consumes the body.
Rust's Tower Service: One Trait for Clients, Servers, and Middleware
Tower's Service trait is a universal API for async requests. It models any 'request -> future<response>' flow, unifying clients, servers, and middleware. Use it for HTTP servers or database clients. The footgun: ignoring `poll_ready` bypasses backpressure.
Terminal User Interfaces (TUIs): GUIs for the Console
A TUI is a graphical interface built from text, offering rich interactivity without leaving the console. Use them for system monitoring (btop), file management, or database clients. The footgun: don't confuse them with CLIs; TUIs are stateful apps.
Rust's Deref Trait: Smart Pointers Acting Like Data
The `Deref` trait lets a "smart pointer" type act like the data it contains, making wrappers transparent. It enables calling an inner type's methods directly on a wrapper, like using `&str` methods on a `String`. Its `deref()` method must never fail.
Go's Functional Options Pattern for Flexible APIs
The functional options pattern uses functions to set optional struct fields, making APIs flexible and readable. It's common for complex constructors like servers or DB clients.

Go's Worker Pool Pattern: Capping Concurrency
A worker pool caps concurrency by using a fixed number of goroutines to process jobs from a queue. Use it for rate-limiting API calls or processing files without spawning unlimited goroutines.
The Builder Pattern: Constructing Complex Objects in Rust
The Builder pattern lets you construct complex objects step-by-step using a chain of method calls. It's crucial in Rust for structs with many optional fields, since the language lacks default arguments.
Rust's `clap`: Build CLIs by Describing Them
`clap` lets you define a Rust struct representing your CLI's arguments, and it generates the parser, help text, and validation. It's used for building any Rust CLI, but its feature-richness can increase binary size over simpler alternatives.

Go Cobra: Build Complex CLIs Like `kubectl`
Cobra gives your Go CLI a command tree, like `git remote add`. It's for apps with nested commands and persistent flags, not just simple tools. The footgun is using it for a single command when Go's `flag` package would suffice.