🐹 Go (Golang)
Intermediate
What are type assertions and type switches in Go?
Answer
A type assertion extracts the concrete value from an interface: s, ok := val.(string). If ok is false, the assertion failed and s is the zero value — the two-value form prevents a panic. A type switch dispatches over multiple possible concrete types in one statement: switch v := x.(type) { case string: ...; case int: ...; default: ... }. Type switches are the idiomatic way to handle heterogeneous data stored as interface{} or any, common when processing JSON, ASTs, or plugin systems.