Go Idioms, Design Patterns & Language Design Interview Questions
16 Go interview questions on go idioms, design patterns & language design, with detailed answers and code.
Go Idioms, Design Patterns & Language Design·Q548·Medium
Functional options are variadic functions of type func(*T) (or func(*T) error ) applied to a value that already holds defaults. The constructor stays backward-compatible…
Go Idioms, Design Patterns & Language Design·Q549·Medium
Package-level var / init() runs eagerly and in a single thread before main , so it is thread-safe with no extra work. The cost is startup time and side effects (network,…
Go Idioms, Design Patterns & Language Design·Q550·Medium
Define a named integer type and a const block that uses iota . Go does not have closed enums: Status(42) compiles, and untyped constants convert implicitly, so…
Go Idioms, Design Patterns & Language Design·Q551·Hard
Inside the function, a variadic parameter xs ...int is a []int . A call like f(1, 2, 3) allocates a new slice. A call like f(s...) passes the caller's slice header as is…
Go Idioms, Design Patterns & Language Design·Q552·Medium
A closure captures variables , not values, by reference. It sees later writes, and its own writes are visible outside. If the closure outlives the stack frame (it is…
Go Idioms, Design Patterns & Language Design·Q553·Medium
Go's designers value code that is easy to read and maintain at scale, where every call site is unambiguous, over expressive power. Each missing feature has an idiomatic…
Go Idioms, Design Patterns & Language Design·Q554·Hard
Func values can only be compared to nil . f == g is a compile error ("func can only be compared to nil"), and map[func()]int is "invalid map key type". The reason:…
Go Idioms, Design Patterns & Language Design·Q555·Hard
No. The Go compiler does not guarantee tail-call elimination and in practice doesn't do it, partly to keep stack traces complete. Each recursive call uses a stack frame.…
Go Idioms, Design Patterns & Language Design·Q556·Medium
A common layout: cmd/<binary>/main.go holds thin mains that only do wiring. internal/ holds most of the code; the compiler enforces that only code rooted at internal's…
Go Idioms, Design Patterns & Language Design·Q557·Medium
Idiomatic DI in Go means constructors that accept interfaces or concrete dependencies as parameters , all wired explicitly in main . No container is needed. The…
Go Idioms, Design Patterns & Language Design·Q558·Medium
In Go these patterns usually collapse into small interfaces plus function types , with no class hierarchies. // Strategy: behavior chosen at runtime through an interface…
Go Idioms, Design Patterns & Language Design·Q559·Hard
A circuit breaker is a state machine. Closed : calls pass and failures are counted. Open : calls fail fast until a cooldown ends. Half-Open : a limited number of probe…
Go Idioms, Design Patterns & Language Design·Q560·Medium
_ is a write-only placeholder. Anything assigned to it is discarded, and it never creates a binding. import ( _ "embed" // needed for //go:embed on string/[]byte vars _…
Go Idioms, Design Patterns & Language Design·Q561·Medium
An anonymous struct is a struct type written inline without a name. Struct tags are raw string metadata on fields, read through reflection ( reflect.StructTag.Get ) by…
Go Idioms, Design Patterns & Language Design·Q562·Hard
The client sends an Idempotency-Key header (a UUID) with non-idempotent requests such as POST. The server atomically reserves the key, runs the operation once, stores…
Go Idioms, Design Patterns & Language Design·Q563·Medium
Concurrency: Go has goroutines on an M:N scheduler plus channels. Blocking-style code scales to 100k+ tasks without function coloring. Java 21+ virtual threads get…