tree: 1ccfe6aed7fd13316619d28447fe0344d3bee29d [path history] [tgz]
  1. doc.go
  3. map.go
  4. merge.go
  5. mergo.go


A helper to merge structs and maps in Golang. Useful for configuration default values, avoiding messy if-statements.

Also a lovely comune (municipality) in the Province of Ancona in the Italian region Marche.

Mergo dall'alto


It is ready for production use. It works fine after extensive use in the wild.

Build Status GoDoc

Important note

Mergo is intended to assign only zero value fields on destination with source value. Since April 6th it works like this. Before it didn‘t work properly, causing some random overwrites. After some issues and PRs I found it didn’t merge as I designed it. Thanks to imdario/mergo#8 overwriting functions were added and the wrong behavior was clearly detected.

If you were using Mergo before April 6th 2015, please check your project works as intended after updating your local copy with go get -u I apologize for any issue caused by its previous behavior and any future bug that Mergo could cause (I hope it won't!) in existing projects after the change (release 0.2.0).

Mergo in the wild


go get

// use in your .go code
import (


You can only merge same-type structs with exported fields initialized as zero value of their type and same-types maps. Mergo won't merge unexported (private) fields but will do recursively any exported one. Also maps will be merged recursively except for structs inside maps (because they are not addressable using Go reflection).

if err := mergo.Merge(&dst, src); err != nil {
    // ...

Additionally, you can map a map[string]interface{} to a struct (and otherwise, from struct to map), following the same restrictions as in Merge(). Keys are capitalized to find each corresponding exported field.

if err := mergo.Map(&dst, srcMap); err != nil {
    // ...

Warning: if you map a struct to map, it won‘t do it recursively. Don’t expect Mergo to map struct members of your struct as map[string]interface{}. They will be just assigned as values.

More information and examples in godoc documentation.

Nice example

package main

import (

type Foo struct {
	A string
	B int64

func main() {
	src := Foo{
		A: "one",

	dest := Foo{
		A: "two",
		B: 2,

	mergo.Merge(&dest, src)

	// Will print
	// {two 2}

Note: if test are failing due missing package, please execute:

go get

Contact me

If I can help you, you have an idea or you are using Mergo in your projects, don't hesitate to drop me a line (or a pull request): @im_dario


Written by Dario Castañé.


BSD 3-Clause license, as Go language.