Go reflection lets a program inspect and manipulate types and values at runtime through the reflect package. You call reflect.TypeOf to get a reflect.Type that describes a type and reflect.ValueOf to get a reflect.Value that wraps the data. Use it for generic tooling such as encoders, validators and ORMs, and prefer generics or interfaces when the types are known at compile time.
Go also has lighter forms of introspection: type assertions, type switches and the %T and %v verbs. Reflection is the heavy tool for the cases they cannot cover.
reflect.TypeOf(x) returns static type information. reflect.ValueOf(x) returns the runtime value. Both take an any, so the argument is converted to an interface first.
Tag.Get("json") returns the value stored under the json key. Unexported fields can be listed as metadata, but calling Interface() on their values panics, which is why the example checks IsExported first.
A Type is the precise type, such as main.User or []string. A Kind is the underlying category: struct, slice, map, ptr, int, func and so on. Write code against Kind when you handle many types the same way.
reflect.Pointer is the current name for reflect.Ptr, which is kept for compatibility.
You can only modify a value that is addressable. Passing a copy to ValueOf gives you something you cannot set, so pass a pointer and call Elem().
Calling Set on a non-settable value or on an unexported field panics. Check CanSet() first when the input is not under your control.
Struct tags drive most reflection-based libraries. This small validator reads a custom tag:
For real projects use a maintained library such as go-playground/validator rather than rolling your own.
Value.MethodByName("Name").Call(args) does the same for methods. A wrong argument count or type panics at runtime, so the compiler no longer protects you.
Use reflection at the edges, for example serialisation, dependency injection or test helpers, and keep the hot path typed. Cache reflect.Type lookups per type instead of recomputing them on every call.
TypeOf returns a reflect.Type that describes the type, including field names, tags and methods. ValueOf returns a reflect.Value that holds the actual data and lets you read or change it.
The value is not settable. Either you passed the struct by value, or the field is unexported. Pass a pointer, use Elem(), and make sure the field name starts with an upper-case letter.
Yes. t.NumMethod() and t.Method(i) list exported methods. For a pointer receiver type, inspect reflect.TypeOf(&x), because the method set of a value type does not include pointer receiver methods.
Reading type information is safe. Reading and writing the same Value from several goroutines needs the same synchronisation as any other shared data.
reflect.TypeOf and reflect.ValueOf give you type and value information at runtime, Kind lets you handle categories of types, and pointers plus Elem() let you modify data. Struct tags make reflection useful for encoders and validators. Because reflection costs speed and type safety, use generics or interfaces first and reserve reflect for genuinely dynamic code.