Reflection

Clear a struct

func clear(v interface{}) {
    p := reflect.ValueOf(v).Elem()
    p.Set(reflect.Zero(p.Type()))
}

Deep equality

The DeepEqual function from the reflect package compares two values deeply. It uses the built-in == operator for basic types. For composite types, it recurses through each field and does similar comparisons.

Why is reflection slow?

Getting type information runs fast. Libraries like jsoniter get the offset values from reflect.StructField in the type info. They use these offsets to grab object values directly.

When you get values through reflection, you get reflect.Value objects. These aren’t reusable. Each call needs to malloc a new value struct. Reflection also involves walking and comparing operations. The comparison work often includes string comparisons. All of this takes CPU time. That’s what makes reflection slow.