Skip to content
Lesson 4 of 7

Step 1 of 4 · Reading · ~1 min

Learn

Generics 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
Up nextError Wrapping and errors.Is / errors.AsErrors, Testing, and Performance

Discussion

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

Sign in to post a comment or reply.

Loading…