Go's reflection API, in the reflect package, lets a program inspect and modify the types and values of variables at runtime. You start with reflect.TypeOf(x) to get a reflect.Type and reflect.ValueOf(x) to get a reflect.Value, then query the kind, fields, methods and tags, or set values through a pointer.
Reflection powers encoding/json, database/sql scanning, validators and dependency injection frameworks. It is slower and less type-safe than ordinary code, so use it when the types are genuinely unknown at compile time, and prefer generics or interfaces otherwise.
The reflect package has three core concepts:
reflect.Type describes a type: its name, kind, fields, methods and element type.reflect.Value holds a value and lets you read, and sometimes change, it.reflect.Kind is the underlying category, such as Int, String, Struct, Slice, Map, Ptr (written Pointer in newer code), Func or Interface.The last line shows the difference between type and kind: Celsius is its own named type, but its kind is float64. Branch on Kind() when you care about the shape of the data, and compare Type values when you care about the exact type.
A reflect.Value exposes typed accessors such as Int(), Float(), String(), Bool(), Len() and Index(i). Calling the wrong accessor for the kind panics, so check Kind() first.
Call v.Interface() to convert a reflect.Value back to an any, and use v.IsValid() and v.IsNil() (only valid for nilable kinds) to avoid panics on empty values. Note that reflect.ValueOf(nil) returns the zero Value, whose Kind() is reflect.Invalid.
Reflection is most useful with structs. You can list fields, read struct tags and see which fields are exported:
You can read unexported fields only through Kind-specific accessors like String(), but calling Interface() or setting them panics. This is why libraries such as encoding/json ignore unexported fields. You can also look a field up by name with t.FieldByName("Name").
A reflect.Value is settable only if it is addressable, which means you must pass a pointer and call Elem() to get the value it points to. Check with CanSet() before assigning.
If you pass cfg by value, reflect.ValueOf(cfg) is not addressable and SetInt panics with "using unaddressable value". The pointer-then-Elem() pattern is the single most important rule of mutating through reflection.
You can find methods by name and call them with reflect.Value arguments:
Only exported methods are visible. Call panics if the argument count or types do not match, so validate with m.Type().NumIn() and m.Type().In(i) when the input comes from outside your code.
Reflection allocates, defeats inlining and moves type checks from compile time to runtime. Operations through reflect are typically many times slower than direct code, so keep reflection out of hot paths or cache the results (for example, parse struct tags once per type and store them).
Prefer these alternatives when they fit:
| Need | Better choice |
|---|---|
| Same logic for several types | Generics (func Sum[T constraints.Ordered]...) |
| Different behaviour per type | Interfaces and type switches |
| Serialisation | Generated code or encoding/json rather than hand-rolled reflection |
Use reflection for code that must work on arbitrary user-defined types: serialisers, ORMs, validators, test helpers such as reflect.DeepEqual, and command-line flag binding. As the Go proverb says, clear is better than clever.
Type identifies the exact type, including its name and package, such as main.Celsius. Kind is the broad category of the underlying type, such as float64 or struct.
You passed a value instead of a pointer. Call reflect.ValueOf(&x).Elem() to get an addressable, settable value, and check CanSet() first.
You can read their names and types, but you cannot call Interface() or set them. The reflect package enforces Go's export rules at runtime.
It works for most types but is slow, treats nil and empty slices as different, and cannot compare functions. For tests, go-cmp gives clearer diffs; for production comparisons, write explicit equality.
The reflect package gives you Type, Value and Kind to inspect and change data whose type is unknown at compile time. Remember to pass pointers and use Elem() for modification, check Kind() before calling typed accessors, and cache expensive lookups. Reach for generics and interfaces first, and use reflection when you truly need runtime flexibility.