Skip to content
/ rf Public

Reflect utils: utilities missing from the "reflect" package in the Go standard library


Notifications You must be signed in to change notification settings


Repository files navigation


Short for "reflect utils": utilities missing from the "reflect" package in the Go standard library. Small and dependency-free.

See the full documentation at



Walking now avoids stack overflow on cyclic types. More specifically, it avoids infinite recursion when generating walkers for cyclic types. Note that fully walking cyclic types is not yet supported; instead, inner occurrences of a cyclic type are ignored, and only the outermost occurrence is walked. This limitation may be lifted in future versions.


Walking now ignores private fields.


Converted the following tools to generics, for better type safety and efficiency:

  • TypeFilter
  • IfaceFilter
  • ShallowIfaceFilter
  • Appender
  • Trawl
  • TrawlWith

Added KindFilter (untested).

Added Type.


Rename WalkFuncPtrWalkPtrFunc.


Add WalkPtr and WalkFuncPtr. Remove DerefPtr because it made no sense.



  • ValueAddr
  • Interface
  • DeepFields
  • TypeDeepFields
  • OffsetFields
  • TypeOffsetFields
  • Path
  • Rev

Breaking changes:

  • Renamed all functions starting with DerefValue* to begin with Deref*.
  • Replaced CopyPath with Path.Copy.

DeepFields and TypeDeepFields is a particularly useful addition, as it supports "flattening" structs, simplifying most use cases that involve struct iteration.


Add IfaceFilterFor, ShallowIfaceFilterFor.


Reverted breaking change in v0.3.1: IfaceFilter once again allows to visit descendants. Added ShallowIfaceFilter that doesn't visit descendants of a matching node.


Quick breaking change: IfaceFilter visits either self or descendants, not both.


More flexible Filter interface:

  • Previous approach: filter returns bool answering "should visit this node". 2 possible states. Implicitly walks descendants.

  • New approach: filter returns flagset where "should visit this node" and "should walk descendants" are both optional flags. 4 possible states. Walking descendants is now optional.

Renamed Filter.ShouldVisit to Filter.Visit because it's no longer a boolean. This makes it impossible to implement Filter and Walker on the same type, which is probably a good thing due to filter equality rules.

Nop no longer implements Filter.

Replaced True and False with Self, Desc, Both, All.

Replaced Not with InvertSelf.

Renamed DerefLen to Len.


Added Fields and TypeFields for micro-optimizing struct shallow walking.


Walk / GetWalker now support walking into interface{} values, fetching the appropriate cached walker for the given type and filter on the fly.

Added MaybeOr, MaybeAnd, GetTypeFilter, TypeFilterFor for micro-optimizing filter allocations.


Complete revision.

  • Removed useless or rarely-used utils.
  • Added many missing utils.
  • New approach to walking / traversal. The old naive approach walked the entire structure every time. The new approach is to JIT-compile a precise walker that visits just what you need, caching it for a combination of type + filter. This makes walking dramatically more efficient.
  • Added Cache for generating and caching arbitrary type-dependent structures.
  • Renamed from to for brevity.



I'm receptive to suggestions. If this library almost satisfies you but needs changes, open an issue or chat me up. Contacts: