Go

Explain the laws of reflection and settability. What does this print?

Question 466HardGo 1.22 to 1.25
type Config struct {
	Port int
	host string
}

c := Config{Port: 80}
v := reflect.ValueOf(c)
fmt.Println(v.Field(0).CanSet())

p := reflect.ValueOf(&c).Elem()
fmt.Println(p.Field(0).CanSet(), p.Field(1).CanSet())
p.Field(0).SetInt(8080)
fmt.Println(c.Port, p.Kind(), reflect.TypeOf(&c).Kind())

Output:

false
true false
8080 struct ptr

Laws: (1) reflection goes from interface value to reflection object (TypeOf/ValueOf); (2) back via v.Interface(); (3) to modify a Value it must be settable — addressable and not obtained through unexported fields. ValueOf(c) holds a copy, so nothing is settable; you need ValueOf(&c).Elem(). Unexported fields can be read (v.Field(1).String()) but not set or Interface()d.

Kind vs Type: Kind is the underlying category (struct, ptr, slice); Type is the named type (main.Config). Calling SetInt on a non-int Kind or non-settable value panics — reflection moves type errors from compile time to runtime. Interviewers like hearing: reflection is slow (allocations, no inlining), so prefer generics or code generation where possible; stdlib uses it for encoding/json, fmt, text/template.

More on Standard Library, HTTP & Systems Design in Go

All 35 Standard Library, HTTP & Systems Design in Go questions