talira

Merge structs and maps in Go

open sourceBSD-3-Clausefree

Mergo is a Go library that merges structs and maps of the same type, filling zero-value fields with defaults, which helps with configuration defaults without long chains of if statements. It merges exported fields recursively, can override values, and supports transformers for custom types; it doesn't merge unexported fields or structs inside maps. Its maintainer calls it stable and frozen: new features wait for a future v2. Since 1.0.0 its import path is dario.cat/mergo (formerly github.com/imdario/mergo). It's BSD-3-Clause licensed.

Problems it solves

  • apply default values to a config struct in Go
  • merge two Go structs without writing field-by-field code
  • combine a map of overrides into my Go settings

Features

  • merge same-type structs and maps
  • fill zero-value fields with defaults
  • override mode
  • transformers for custom types

Limitations

  • Frozen: no new features until a future v21