Skip to content
Reflection
step 1/4

Reading — step 1 of 4

Learn

~1 min readGenerics and Reflection

Go's reflect package inspects types at runtime. Used heavily by JSON decoders, ORMs, RPC frameworks.

go

Iterate struct fields:

go

Struct tags — metadata read by libraries:

go

This is how encoding/json knows to lowercase field names.

Setting fields (must use a pointer + Elem):

go

Caveats:

  • ~100x slower than direct access
  • Loses compile-time type checking — bugs surface at runtime
  • Use only when truly needed (mostly for libraries, not application code)

reflect.DeepEqual is useful for tests:

go

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…