Behavioural Mid Level

How have generics changed the way you write Go? Give an example where generics improved your code and one where you chose not to use them.

Quick Tip

Show balanced judgement: "I use generics for utility functions like slices.Contains and type-safe result types. I avoid them when a concrete type or small interface is clearer — Go readability matters more than type gymnastics."

What good answers include

Generics (Go 1.18+) eliminate the need for interface{}/any casts and code generation for type-safe collections and utilities. Good use cases: generic data structures (maps, sets, queues), utility functions (Map, Filter, Contains), and reducing boilerplate across similar types. Cases to avoid generics: when a simple interface suffices, when it makes code harder to read, or when the function only works with one or two types. Strong candidates show restraint — generics are a tool, not a goal.

What interviewers are looking for

Tests whether candidates adopt features thoughtfully. Those who use generics everywhere are fighting Go idioms. Those who refuse to use them are missing genuine improvements. Look for practical examples with clear trade-off reasoning.

← All Go questions