Reflection in Go, provided by the standard reflect package, lets a program inspect and modify values and types at runtime. It is used by libraries that must work with arbitrary types, such as encoding/json, database/sql scanners, ORMs, validators, and dependency injection frameworks.
Reflection is powerful but slower than ordinary code and bypasses compile-time type checks, so use it only when generics, interfaces, or code generation cannot solve the problem.
reflect.TypeOf returns a reflect.Type describing a value's type, and reflect.ValueOf returns a reflect.Value holding the value itself. Every reflect.Type has a Kind (such as Int, Struct, Slice, Ptr) that tells you the underlying category.
Type is the named type (main.Celsius), while Kind is the underlying kind (float64). Check Kind when writing code that handles many types.
Struct tags are the most common reason to use reflection. Libraries read them to decide how to encode, validate, or map a field.
Unexported fields can be read as metadata but their values cannot be accessed through Interface() or set; doing so panics.
A reflect.Value is settable only if it was obtained from a pointer and refers to an addressable, exported field. Pass a pointer, call Elem(), and check CanSet().
reflect.ValueOf(x).SetInt(20) without a pointer panics, because the value is a copy and not addressable.
Only exported methods are visible. Call panics if the arguments do not match the method signature, so validate with m.Type() when input is untrusted.
A recursive function over Kind handles any value. This one counts every integer found in nested slices, maps, and structs.
Reflection has three costs: it is noticeably slower than direct code, it moves type errors from compile time to runtime panics, and it makes code harder to read.
| Instead of reflection | Prefer |
|---|---|
| Operating on several known types | A type switch or an interface |
| Generic containers and helpers | Generics (Go 1.18+) |
| Repetitive per-type boilerplate | Code generation with go generate |
| Choosing behaviour by type | Methods on an interface |
If you must use it, cache the results of reflect.TypeOf and field lookups per type instead of recomputing them for every call, and recover from panics at API boundaries.
Type is the exact type, including its name and package, while Kind is the underlying category such as struct or int. A type Celsius float64 has type Celsius and kind float64.
You tried to set a value obtained from a non-pointer. Pass a pointer to reflect.ValueOf, call Elem(), and confirm CanSet() is true.
It is several times slower than direct access and allocates more, which matters in hot paths. For most configuration, serialization, and setup code the cost is acceptable.
You can see their names and types, but you cannot read them through Interface() or set them. Doing so panics unless you bypass safety with unsafe, which is not recommended.
The reflect package gives you TypeOf, ValueOf, Kind, struct tags, settable values, and dynamic method calls. Use it for libraries that must handle unknown types, pass pointers to modify values, and reach for generics, interfaces, or code generation first.