...
1# Mergo
2
3[![GitHub release][5]][6]
4[![GoCard][7]][8]
5[![Test status][1]][2]
6[![OpenSSF Scorecard][21]][22]
7[![OpenSSF Best Practices][19]][20]
8[![Coverage status][9]][10]
9[![Sourcegraph][11]][12]
10[![FOSSA status][13]][14]
11
12[![GoDoc][3]][4]
13[![Become my sponsor][15]][16]
14[![Tidelift][17]][18]
15
16[1]: https://github.com/imdario/mergo/workflows/tests/badge.svg?branch=master
17[2]: https://github.com/imdario/mergo/actions/workflows/tests.yml
18[3]: https://godoc.org/github.com/imdario/mergo?status.svg
19[4]: https://godoc.org/github.com/imdario/mergo
20[5]: https://img.shields.io/github/release/imdario/mergo.svg
21[6]: https://github.com/imdario/mergo/releases
22[7]: https://goreportcard.com/badge/imdario/mergo
23[8]: https://goreportcard.com/report/github.com/imdario/mergo
24[9]: https://coveralls.io/repos/github/imdario/mergo/badge.svg?branch=master
25[10]: https://coveralls.io/github/imdario/mergo?branch=master
26[11]: https://sourcegraph.com/github.com/imdario/mergo/-/badge.svg
27[12]: https://sourcegraph.com/github.com/imdario/mergo?badge
28[13]: https://app.fossa.io/api/projects/git%2Bgithub.com%2Fimdario%2Fmergo.svg?type=shield
29[14]: https://app.fossa.io/projects/git%2Bgithub.com%2Fimdario%2Fmergo?ref=badge_shield
30[15]: https://img.shields.io/github/sponsors/imdario
31[16]: https://github.com/sponsors/imdario
32[17]: https://tidelift.com/badges/package/go/github.com%2Fimdario%2Fmergo
33[18]: https://tidelift.com/subscription/pkg/go-github.com-imdario-mergo
34[19]: https://bestpractices.coreinfrastructure.org/projects/7177/badge
35[20]: https://bestpractices.coreinfrastructure.org/projects/7177
36[21]: https://api.securityscorecards.dev/projects/github.com/imdario/mergo/badge
37[22]: https://api.securityscorecards.dev/projects/github.com/imdario/mergo
38
39A helper to merge structs and maps in Golang. Useful for configuration default values, avoiding messy if-statements.
40
41Mergo merges same-type structs and maps by setting default values in zero-value fields. Mergo won't merge unexported (private) fields. It will do recursively any exported one. It also won't merge structs inside maps (because they are not addressable using Go reflection).
42
43Also a lovely [comune](http://en.wikipedia.org/wiki/Mergo) (municipality) in the Province of Ancona in the Italian region of Marche.
44
45## Status
46
47It is ready for production use. [It is used in several projects by Docker, Google, The Linux Foundation, VMWare, Shopify, Microsoft, etc](https://github.com/imdario/mergo#mergo-in-the-wild).
48
49### Important note
50
51Please keep in mind that a problematic PR broke [0.3.9](//github.com/imdario/mergo/releases/tag/0.3.9). I reverted it in [0.3.10](//github.com/imdario/mergo/releases/tag/0.3.10), and I consider it stable but not bug-free. Also, this version adds support for go modules.
52
53Keep in mind that in [0.3.2](//github.com/imdario/mergo/releases/tag/0.3.2), Mergo changed `Merge()`and `Map()` signatures to support [transformers](#transformers). I added an optional/variadic argument so that it won't break the existing code.
54
55If you were using Mergo before April 6th, 2015, please check your project works as intended after updating your local copy with ```go get -u github.com/imdario/mergo```. I apologize for any issue caused by its previous behavior and any future bug that Mergo could cause in existing projects after the change (release 0.2.0).
56
57### Donations
58
59If Mergo is useful to you, consider buying me a coffee, a beer, or making a monthly donation to allow me to keep building great free software. :heart_eyes:
60
61<a href='https://ko-fi.com/B0B58839' target='_blank'><img height='36' style='border:0px;height:36px;' src='https://az743702.vo.msecnd.net/cdn/kofi1.png?v=0' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a>
62<a href="https://liberapay.com/dario/donate"><img alt="Donate using Liberapay" src="https://liberapay.com/assets/widgets/donate.svg"></a>
63<a href='https://github.com/sponsors/imdario' target='_blank'><img alt="Become my sponsor" src="https://img.shields.io/github/sponsors/imdario?style=for-the-badge" /></a>
64
65### Mergo in the wild
66
67- [moby/moby](https://github.com/moby/moby)
68- [kubernetes/kubernetes](https://github.com/kubernetes/kubernetes)
69- [vmware/dispatch](https://github.com/vmware/dispatch)
70- [Shopify/themekit](https://github.com/Shopify/themekit)
71- [imdario/zas](https://github.com/imdario/zas)
72- [matcornic/hermes](https://github.com/matcornic/hermes)
73- [OpenBazaar/openbazaar-go](https://github.com/OpenBazaar/openbazaar-go)
74- [kataras/iris](https://github.com/kataras/iris)
75- [michaelsauter/crane](https://github.com/michaelsauter/crane)
76- [go-task/task](https://github.com/go-task/task)
77- [sensu/uchiwa](https://github.com/sensu/uchiwa)
78- [ory/hydra](https://github.com/ory/hydra)
79- [sisatech/vcli](https://github.com/sisatech/vcli)
80- [dairycart/dairycart](https://github.com/dairycart/dairycart)
81- [projectcalico/felix](https://github.com/projectcalico/felix)
82- [resin-os/balena](https://github.com/resin-os/balena)
83- [go-kivik/kivik](https://github.com/go-kivik/kivik)
84- [Telefonica/govice](https://github.com/Telefonica/govice)
85- [supergiant/supergiant](supergiant/supergiant)
86- [SergeyTsalkov/brooce](https://github.com/SergeyTsalkov/brooce)
87- [soniah/dnsmadeeasy](https://github.com/soniah/dnsmadeeasy)
88- [ohsu-comp-bio/funnel](https://github.com/ohsu-comp-bio/funnel)
89- [EagerIO/Stout](https://github.com/EagerIO/Stout)
90- [lynndylanhurley/defsynth-api](https://github.com/lynndylanhurley/defsynth-api)
91- [russross/canvasassignments](https://github.com/russross/canvasassignments)
92- [rdegges/cryptly-api](https://github.com/rdegges/cryptly-api)
93- [casualjim/exeggutor](https://github.com/casualjim/exeggutor)
94- [divshot/gitling](https://github.com/divshot/gitling)
95- [RWJMurphy/gorl](https://github.com/RWJMurphy/gorl)
96- [andrerocker/deploy42](https://github.com/andrerocker/deploy42)
97- [elwinar/rambler](https://github.com/elwinar/rambler)
98- [tmaiaroto/gopartman](https://github.com/tmaiaroto/gopartman)
99- [jfbus/impressionist](https://github.com/jfbus/impressionist)
100- [Jmeyering/zealot](https://github.com/Jmeyering/zealot)
101- [godep-migrator/rigger-host](https://github.com/godep-migrator/rigger-host)
102- [Dronevery/MultiwaySwitch-Go](https://github.com/Dronevery/MultiwaySwitch-Go)
103- [thoas/picfit](https://github.com/thoas/picfit)
104- [mantasmatelis/whooplist-server](https://github.com/mantasmatelis/whooplist-server)
105- [jnuthong/item_search](https://github.com/jnuthong/item_search)
106- [bukalapak/snowboard](https://github.com/bukalapak/snowboard)
107- [containerssh/containerssh](https://github.com/containerssh/containerssh)
108- [goreleaser/goreleaser](https://github.com/goreleaser/goreleaser)
109- [tjpnz/structbot](https://github.com/tjpnz/structbot)
110
111## Install
112
113 go get github.com/imdario/mergo
114
115 // use in your .go code
116 import (
117 "github.com/imdario/mergo"
118 )
119
120## Usage
121
122You 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. It won't merge empty structs value as [they are zero values](https://golang.org/ref/spec#The_zero_value) too. Also, maps will be merged recursively except for structs inside maps (because they are not addressable using Go reflection).
123
124```go
125if err := mergo.Merge(&dst, src); err != nil {
126 // ...
127}
128```
129
130Also, you can merge overwriting values using the transformer `WithOverride`.
131
132```go
133if err := mergo.Merge(&dst, src, mergo.WithOverride); err != nil {
134 // ...
135}
136```
137
138Additionally, 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.
139
140```go
141if err := mergo.Map(&dst, srcMap); err != nil {
142 // ...
143}
144```
145
146Warning: 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.
147
148Here is a nice example:
149
150```go
151package main
152
153import (
154 "fmt"
155 "github.com/imdario/mergo"
156)
157
158type Foo struct {
159 A string
160 B int64
161}
162
163func main() {
164 src := Foo{
165 A: "one",
166 B: 2,
167 }
168 dest := Foo{
169 A: "two",
170 }
171 mergo.Merge(&dest, src)
172 fmt.Println(dest)
173 // Will print
174 // {two 2}
175}
176```
177
178Note: if test are failing due missing package, please execute:
179
180 go get gopkg.in/yaml.v3
181
182### Transformers
183
184Transformers allow to merge specific types differently than in the default behavior. In other words, now you can customize how some types are merged. For example, `time.Time` is a struct; it doesn't have zero value but IsZero can return true because it has fields with zero value. How can we merge a non-zero `time.Time`?
185
186```go
187package main
188
189import (
190 "fmt"
191 "github.com/imdario/mergo"
192 "reflect"
193 "time"
194)
195
196type timeTransformer struct {
197}
198
199func (t timeTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
200 if typ == reflect.TypeOf(time.Time{}) {
201 return func(dst, src reflect.Value) error {
202 if dst.CanSet() {
203 isZero := dst.MethodByName("IsZero")
204 result := isZero.Call([]reflect.Value{})
205 if result[0].Bool() {
206 dst.Set(src)
207 }
208 }
209 return nil
210 }
211 }
212 return nil
213}
214
215type Snapshot struct {
216 Time time.Time
217 // ...
218}
219
220func main() {
221 src := Snapshot{time.Now()}
222 dest := Snapshot{}
223 mergo.Merge(&dest, src, mergo.WithTransformers(timeTransformer{}))
224 fmt.Println(dest)
225 // Will print
226 // { 2018-01-12 01:15:00 +0000 UTC m=+0.000000001 }
227}
228```
229
230## Contact me
231
232If 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](https://twitter.com/im_dario)
233
234## About
235
236Written by [Dario Castañé](http://dario.im).
237
238## License
239
240[BSD 3-Clause](http://opensource.org/licenses/BSD-3-Clause) license, as [Go language](http://golang.org/LICENSE).
241
242[](https://app.fossa.io/projects/git%2Bgithub.com%2Fimdario%2Fmergo?ref=badge_large)
View as plain text