GO
157 bites tagged GO — interview questions with model answers, and 60-second explainers.
Go Test Coverage: Rewriting Source to See What's Untested
Go's coverage tool rewrites your source code, adding counters to see what's executed during tests. It's a powerful way to find untested code, but remember: high coverage doesn't guarantee your tests are actually checking for correctness.
Go Table-Driven Tests: Test More with Less Code
Instead of copy-pasting tests, define inputs and expected outputs in a table (a slice or map) and loop through them. This is the idiomatic Go way to test functions with many edge cases. The main footgun is a closure bug in parallel tests; re-shadow the.
Foreign Function Interface (FFI): Calling Other Languages
Think of an FFI as a universal adapter, letting your program call functions written in another language. It's how modern code in Rust or Go can reuse battle-tested C libraries for tasks like graphics or system calls, avoiding a complete rewrite.
Go's `context` Package: Propagating Cancellation and Deadlines
Go's `context` package is a lifeline for requests, carrying cancellation signals, deadlines, and values across function calls and goroutines. It's essential for I/O-bound operations to prevent resource leaks.
Regex Engines: Backtracking vs. Finite Automata
A backtracking regex engine tries one path at a time, which can be fast but also exponentially slow. A finite-automata engine (like Go's) checks all paths at once, guaranteeing linear time. The footgun is using a backtracking engine on untrusted user input.
Go's `net/http`: A Production-Ready Web Server
Go's `net/http` package provides a powerful, production-ready web server without external frameworks. You build services by creating handlers—functions that process a request and write a response. It's ideal for APIs and microservices.
Buffered I/O: Batch System Calls for Speed
Buffered I/O batches many small reads or writes into fewer, larger system calls, trading a small amount of memory for a huge speed boost. It's essential for tasks like writing log files line-by-line, preventing a system call for every single line.
Go vs. Rust: Why String Indexing Is Tricky
Rust prevents direct string indexing to force correctness, while Go treats strings as raw byte slices. This matters for non-ASCII text where characters span multiple bytes. The footgun: Go's `s[i]` can corrupt data; Rust's `&s[..i]` can panic.
Go's Time: Wall Clocks vs. Monotonic Clocks
Go separates telling time (wall clock) from measuring it (monotonic clock) to prevent errors from system clock changes. Use `time.Sub` for reliable timing and `time.Format` for display. The footgun: formatting uses a magic date, not YYYY-MM-DD.
Go-Style vs. GNU-Style Flag Parsing
Go's command-line parser is stricter than the familiar GNU style, not distinguishing short/long flags or allowing them after arguments. This is key when porting Go CLIs to Rust to maintain user experience.
Go's `os` Package: Your File System Toolkit
Go's `os` package is your universal remote for the file system. Use `ReadFile` for quick access or open a `File` object for finer control. It's essential for logs and configs. Forgetting to `Close()` a file leaks resources and can crash your program.
Go's Race Detector: Find Concurrency Bugs at Runtime
The Go race detector finds data races by watching memory access at runtime. Use `go test -race` in CI or on a canary instance, but remember: it only catches races that actually execute. If your tests don't trigger the race, it won't be found.
Go Benchmarking: Measure, Don't Guess
Go's benchmark runner finds stable performance numbers by repeatedly calling your code in a loop controlled by b.N. Use it to optimize hot paths or compare algorithm implementations. Forgetting b.ResetTimer() will include setup costs, skewing your results.
Go Build: From Source Code to Executable
`go build` is your factory for turning Go source into a runnable program. It compiles your packages and their dependencies into a single executable. Use it to create a binary for deployment, but don't confuse it with `go install` which puts the file in your.
Go's Memory Model: Don't Be Clever
Go guarantees your program behaves predictably—as if on one CPU—if you prevent data races. Use channels or `sync` primitives to serialize access when goroutines share data. The footgun is relying on timing instead of explicit synchronization.
Go Channels: Buffered vs. Unbuffered
Unbuffered channels are a synchronous rendezvous, blocking until both sender and receiver are ready. Buffered channels are an async mailbox, letting senders drop messages and go. The footgun is using a buffer to hide a deadlock instead of fixing it.
Go's Panic/Recover: For Exceptional Errors Only
Go's panic/recover is a last-resort error mechanism, not a try/catch replacement. A panic unwinds a goroutine's stack until a recover in a defer'd function catches it. It's used to keep a server alive when one request fails catastrophically.
Go Error Wrapping: Preserving Context, Not Just Text
Go's error wrapping adds context without losing the original error's type. Use `fmt.Errorf` with `%w` to create a chain of errors, then inspect it with `errors.Is` or `errors.As`. The footgun is using `%v`, which just formats the error as a string.
Go's `error` Interface: Errors Are Values
In Go, an error is any value that can describe its own failure string. Functions like `os.Open` return an `error` to signal problems. The footgun is only checking for `nil` and ignoring the rich, structured data a custom error type can provide.
Go's GC: Low-Latency Collection with Tri-color Marking
Go's GC uses a tri-color algorithm to find unused memory concurrently, avoiding long pauses. It's crucial for low-latency services. The main footgun is breaking the invariant: a 'finished' (black) object must never point to a new (white) one without notifying…
GC vs. Ownership: Two Paths to Memory Safety
Rust's ownership model provides memory safety at compile-time, aiming for C++-level performance without a garbage collector. This makes it ideal for systems programming where resource control is key. The footgun is assuming all "safe" languages are equal.
Stack vs. Heap: Where Go Puts Your Data
The stack is a fast, last-in-first-out region for local, fixed-size data. The heap is slower, flexible memory for dynamic data or values that escape a function's scope.
Go's `internal` Directory: Private by Convention
Go's `internal` directory creates private packages within your module, making them inaccessible to external projects. Use it for helper logic you don't want to support as a public API.
Go's Entry Point: The `main` Package and Function
A Go program's entry point is `package main`. The compiler finds this package and its `main()` function to create a runnable binary. The footgun is naming a library `main`; this name is reserved for executables and will cause build confusion.
Get GO bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.