Documentation Index
Fetch the complete documentation index at: https://mintlify.com/golang/go/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Thereflect package implements run-time reflection, allowing a program to manipulate objects with arbitrary types. The typical use is to take a value with static type interface{} and extract its dynamic type information.
Key concepts:
TypeOf()returns the reflection Type of a valueValueOf()returns a Value representing the run-time dataZero()creates a zero Value for a given Type
Core Types
Type Interface
TheType interface represents a Go type and provides methods to inspect type information.
Key Methods
| Method | Description |
|---|---|
Align() | Returns alignment in bytes |
Kind() | Returns the specific kind of type |
Name() | Returns type’s name within its package |
PkgPath() | Returns the package path |
NumMethod() | Returns number of methods |
Field(i) | Returns i’th struct field |
Elem() | Returns element type (for pointer, array, slice, map, chan) |
Value Struct
Value is the reflection interface to a Go value.
Common Value Methods
| Method | Description |
|---|---|
Kind() | Returns the value’s Kind |
Type() | Returns the value’s Type |
Interface() | Returns value as interface |
IsValid() | Reports whether value is valid |
IsZero() | Reports whether value is zero |
CanSet() | Reports whether value can be changed |
Set(Value) | Sets value (must be settable) |
Elem() | Returns value that interface/pointer contains |
Type-Specific Methods
For integers:Int()- returns as int64SetInt(int64)- sets the value
Float()- returns as float64SetFloat(float64)- sets the value
String()- returns as stringSetString(string)- sets the value
Field(i int)- returns i’th fieldFieldByName(string)- returns field by nameNumField()- returns number of fieldsFields()- returns iterator over fields
Len()- returns lengthIndex(i int)- returns i’th elementSlice(i, j int)- returns slice
MapKeys()- returns slice of map keysMapIndex(key)- returns value for keySetMapIndex(key, val)- sets map entry
Core Functions
TypeOf
Returns the reflection Type of the value in the interface.ValueOf
Returns a new Value initialized to the concrete value stored in the interface.Zero
Returns a Value representing the zero value for the specified type.New
Returns a Value representing a pointer to a new zero value for the specified type.MakeFunc
Returns a new function of the given Type that wraps the function fn.Kind Type
Kind represents the specific kind of type.Struct Tags
StructTag
StructTag is the tag string in a struct field.Common Patterns
Checking Type Kind
Iterating Struct Fields
Creating Dynamic Structs
Modifying Values
Performance Considerations
Best practices:- Cache
reflect.Typeandreflect.Valuewhen possible - Avoid reflection in hot paths
- Use type assertions when the type is known
- Consider code generation for performance-critical code
Related Packages
encoding/json- Uses reflection for marshaling/unmarshalingencoding/xml- Uses reflection for XML processingfmt- Uses reflection for printing valuesdatabase/sql- Uses reflection for scanning rows