Master Go’s reflect Package in One Minute
This article walks beginners through Go's reflect package, covering the core functions TypeOf and ValueOf, the reflect.Kind constants, a step‑by‑step parsing flow, concrete handling of structs, maps, slices, pointers, primitive types, and the full set of Type and Value methods.
Basic functions
reflect.TypeOf returns the dynamic type of a value, and reflect.ValueOf returns the dynamic value.
func TypeOf(i interface{}) Type</code><code>func ValueOf(i interface{}) Valuereflect.Kind
Calling Kind() on a reflect.Type or reflect.Value yields a reflect.Kind constant that identifies the underlying kind (Invalid, Bool, Int, Int8, Int16, Int32, Int64, Uint, Uint8, Uint16, Uint32, Uint64, Uintptr, Float32, Float64, Complex64, Complex128, Array, Chan, Func, Interface, Map, Ptr, Slice, String, Struct, UnsafePointer).
const ( Invalid Kind = iota Bool Int Int8 Int16 Int32 Int64 Uint Uint8 Uint16 Uint32 Uint64 Uintptr Float32 Float64 Complex64 Complex128 Array Chan Func Interface Map Ptr Slice String Struct UnsafePointer )Parsing algorithm
The typical parsing flow starts with reflect.TypeOf(data) and reflect.ValueOf(data). If the Kind is Ptr or Interface, call Elem() to obtain the underlying element and repeat. For composite kinds Slice, Array, Map, and Struct, retrieve their element types (Elem for Slice/Array, Key/Elem for Map, Field(i) for Struct) and recursively process each element or field. The recursion stops when the Kind is a primitive type (Bool, Int, Uint, Float, Complex, String) or one of Func, Chan, UnsafePointer.
Concrete type handling examples
Struct
// iterate struct fields
for i := 0; i < rt.NumField(); i++ {
sf := rt.Field(i) // StructField
fv := rv.Field(i) // Value of the field
}
// iterate struct methods
for i := 0; i < rt.NumMethod(); i++ {
m := rt.Method(i) // Method
mv := rv.Method(i) // Value of the method
}Map
for _, k := range rv.MapKeys() {
// k is a Value representing the map key
v := rv.MapIndex(k) // value for the key
}
size := rv.Len() // map size
rv.SetMapIndex(k, v) // assign entrySlice / Array
for i := 0; i < rv.Len(); i++ {
elem := rv.Index(i) // element Value
}
cap := rv.Cap() // capacity (Slice only)
rv.SetCap(newCap) // adjust capacity
sub := rv.Slice(i, j) // sub‑slice ValuePtr / Interface
if !rv.IsNil() {
elem := rv.Elem() // actual element value
}Primitive values
b := rv.Bool()
i := rv.Int()
u := rv.Uint()
f := rv.Float()
c := rv.Complex()
s := rv.String()
// setting values (type‑matched)
rv.SetBool(true)
rv.SetInt(42)
rv.SetUint(7)
rv.SetFloat(3.14)
rv.SetComplex(complex(1,2))
rv.SetString("text")
rv.SetBytes([]byte{0x1,0x2})Methods shared by reflect.Type and reflect.Value
Kind : returns the Kind of the object.
NumMethod : number of methods on a struct (including unexported).
MethodByName : find method by name.
Field , FieldByIndex , FieldByName , FieldByNameFunc : access struct fields.
NumField : number of fields in a struct.
Methods unique to reflect.Type
Align, FieldAlign, Name, PkgPath, Size, String
Implements, AssignableTo, ConvertibleTo, Comparable
ChanDir, IsVariadic
Elem, In, Out, NumIn, NumOut
Key, Len
Methods unique to reflect.Value
Addr, Bool, Bytes, Call, CallSlice, CanAddr, CanInterface, CanSet, Cap, Close, Complex, Convert, Elem, Float, Index, Int, Interface, IsNil, IsValid, Len, MapIndex, MapKeys, OverflowComplex, OverflowFloat, OverflowInt, OverflowUint, Pointer, Recv, Send, Set, SetBool, SetBytes, SetCap, SetMapIndex, SetUint, SetString, Slice, String, TryRecv, TrySend, Type, UnsafeAddr
Reference
Type and Value methods: https://www.cnblogs.com/ksir16/p/9040656.html
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Golang Shines
We share daily the latest Golang technical articles, practical resources, language news, tutorials, and real-world projects to help everyone learn and improve.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
