The reflect package is Go's standard library for dynamic programming: it lets code work with values whose types are unknown until runtime. Beyond reading types and fields, it can compare arbitrary values with reflect.DeepEqual, build slices, maps and functions on the fly, call methods by name, and walk nested data structures. It trades compile-time safety and speed for flexibility, so use it only where generics and interfaces fall short.
This article focuses on the dynamic-programming side of the package: creating values, deep comparison, generic walkers and calling code by name. For the basics of TypeOf and ValueOf, see the companion article on reflection and introspection.
| Item | Purpose |
|---|---|
reflect.Type | Describes a type: name, kind, fields, methods, element type |
reflect.Value | Holds a value and lets you read, set or call it |
reflect.Kind | Category such as Struct, Slice, Map, Func |
reflect.StructTag | Parsed struct tag with Get and Lookup |
reflect.DeepEqual | Recursive equality for any two values |
Every reflect.Value operation is checked at runtime. Calling Int() on a string value, or Set on a non-addressable value, panics instead of failing to compile.
You can build values from a reflect.Type without knowing it statically.
reflect.New returns a pointer value, so Elem() gives you a settable value of the type itself. Similar helpers exist for channels (MakeChan), functions (MakeFunc) and new types such as reflect.SliceOf, reflect.MapOf and reflect.StructOf.
== does not work on slices, maps or functions. reflect.DeepEqual compares them recursively.
Note the nil versus empty slice difference. In tests, prefer slices.Equal, maps.Equal or the go-cmp library, which give readable diffs and let you control comparison rules. DeepEqual also treats unexported fields as part of the comparison and is not safe for values with cycles you did not expect.
Only exported methods are visible. Check IsValid() before calling, and validate argument types with m.Type().In(i) if the input is untrusted. Plugin systems, RPC frameworks and test helpers use this pattern.
A recursive function over Kind lets you process any structure, for example to redact fields tagged as secret.
Call it with a pointer, redact(reflect.ValueOf(&data)), so the fields are settable. Iterating with range over an integer needs Go 1.22 or later.
Go 1.18 generics replace many old uses of reflection. A generic function is type checked and fast:
Reflection remains the right tool when the shape of the data is only known at runtime, such as decoding JSON into arbitrary structs, building a CLI from struct tags, or writing a dependency injection container.
It works but gives no explanation when it fails. go-cmp (cmp.Diff) shows exactly where values differ and can ignore fields or unexported state.
It is typically many times slower and allocates more, because every access goes through type descriptors and runtime checks. Benchmark your own hot paths and cache reflect.Type data per type.
You can read their names and types, but not get or set their values through reflect without the unsafe package. This is intentional and unsafe tricks should be avoided.
Yes. Type parameters are resolved to concrete types at runtime, so reflect.TypeOf on a generic value reports the instantiated type, such as main.Stack[int].
The reflect package supports dynamic programming in Go: creating values and containers, comparing data with DeepEqual, calling methods by name and walking arbitrary structures. Everything is checked at runtime, so validate input, check IsValid and CanSet, and prefer generics or interfaces whenever the types are known at compile time.