🐹 Go (Golang)
Intermediate
What is reflection in Go and when should you use it?
Answer
The reflect package lets you inspect and manipulate values, types, and struct tags at runtime. reflect.TypeOf(x) returns the type and reflect.ValueOf(x) returns a reflected value. Reflection is used internally by encoding packages (encoding/json, encoding/xml) to inspect struct field names and tags like json:"name". It is also used for ORM libraries and dependency injection frameworks. However, reflection is slow, bypasses static type checking, and makes code harder to read — prefer generics or interface-based designs and use reflection only when the type is genuinely unknown at compile time.