mergo alternatives and similar packages
Based on the "Utilities" category.
Alternatively, view mergo alternatives based on common mentions on social networks and blogs.
-
项目文档
🚀Vite+Vue3+Gin的开发基础平台,支持TS和JS混用。它集成了JWT鉴权、权限管理、动态路由、显隐可控组件、分页封装、多点登录拦截、资源权限、上传下载、代码生成器【可AI辅助】、表单生成器和可配置的导入导出等开发必备功能。 -
excelize
Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets -
Kopia
Cross-platform backup tool for Windows, macOS & Linux with fast, incremental backups, client-side end-to-end encryption, compression and data deduplication. CLI and GUI included. -
goreporter
A Golang tool that does static analysis, unit testing, code review and generate code quality report. -
create-go-app
✨ A complete and self-contained solution for developers of any qualification to create a production-ready project with backend (Go), frontend (JavaScript, TypeScript) and deploy automation (Ansible, Docker) by running only one CLI command. -
EaseProbe
A simple, standalone, and lightweight tool that can do health/status checking, written in Go. -
filetype
Fast, dependency-free Go package to infer binary file types based on the magic numbers header signature -
boilr
:zap: boilerplate template manager that generates files or directories from template repositories -
beaver
💨 A real time messaging system to build a scalable in-app notifications, multiplayer games, chat apps in web and mobile apps. -
go-underscore
Helpfully Functional Go - A useful collection of Go utilities. Designed for programmer happiness.
InfluxDB - Purpose built for real-time analytics at any scale.
Do you think we are missing an alternative of mergo or a related project?
Popular Comparisons
README
Mergo
A helper to merge structs and maps in Golang. Useful for configuration default values, avoiding messy if-statements.
Mergo 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).
Also a lovely comune (municipality) in the Province of Ancona in the Italian region of Marche.
Status
It is ready for production use. It is used in several projects by Docker, Google, The Linux Foundation, VMWare, Shopify, Microsoft, etc.
Important note
Please 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.
Keep 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. I added an optional/variadic argument so that it won't break the existing code.
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 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).
Donations
If 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:
Mergo in the wild
- cli/cli
- moby/moby
- kubernetes/kubernetes
- vmware/dispatch
- Shopify/themekit
- imdario/zas
- matcornic/hermes
- OpenBazaar/openbazaar-go
- kataras/iris
- michaelsauter/crane
- go-task/task
- sensu/uchiwa
- ory/hydra
- sisatech/vcli
- dairycart/dairycart
- projectcalico/felix
- resin-os/balena
- go-kivik/kivik
- Telefonica/govice
- [supergiant/supergiant](supergiant/supergiant)
- SergeyTsalkov/brooce
- soniah/dnsmadeeasy
- ohsu-comp-bio/funnel
- EagerIO/Stout
- lynndylanhurley/defsynth-api
- russross/canvasassignments
- rdegges/cryptly-api
- casualjim/exeggutor
- divshot/gitling
- RWJMurphy/gorl
- andrerocker/deploy42
- elwinar/rambler
- tmaiaroto/gopartman
- jfbus/impressionist
- Jmeyering/zealot
- godep-migrator/rigger-host
- Dronevery/MultiwaySwitch-Go
- thoas/picfit
- mantasmatelis/whooplist-server
- jnuthong/item_search
- bukalapak/snowboard
- containerssh/containerssh
- goreleaser/goreleaser
- tjpnz/structbot
Install
go get github.com/imdario/mergo
// use in your .go code
import (
"github.com/imdario/mergo"
)
Usage
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. It won't merge empty structs value as they are zero values too. 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 {
// ...
}
Also, you can merge overwriting values using the transformer WithOverride
.
if err := mergo.Merge(&dst, src, mergo.WithOverride); 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.
Here is a nice example:
package main
import (
"fmt"
"github.com/imdario/mergo"
)
type Foo struct {
A string
B int64
}
func main() {
src := Foo{
A: "one",
B: 2,
}
dest := Foo{
A: "two",
}
mergo.Merge(&dest, src)
fmt.Println(dest)
// Will print
// {two 2}
}
Note: if test are failing due missing package, please execute:
go get gopkg.in/yaml.v3
Transformers
Transformers 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
?
package main
import (
"fmt"
"github.com/imdario/mergo"
"reflect"
"time"
)
type timeTransformer struct {
}
func (t timeTransformer) Transformer(typ reflect.Type) func(dst, src reflect.Value) error {
if typ == reflect.TypeOf(time.Time{}) {
return func(dst, src reflect.Value) error {
if dst.CanSet() {
isZero := dst.MethodByName("IsZero")
result := isZero.Call([]reflect.Value{})
if result[0].Bool() {
dst.Set(src)
}
}
return nil
}
}
return nil
}
type Snapshot struct {
Time time.Time
// ...
}
func main() {
src := Snapshot{time.Now()}
dest := Snapshot{}
mergo.Merge(&dest, src, mergo.WithTransformers(timeTransformer{}))
fmt.Println(dest)
// Will print
// { 2018-01-12 01:15:00 +0000 UTC m=+0.000000001 }
}
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
About
Written by Dario Castañé.
License
BSD 3-Clause license, as Go language.
*Note that all licence references and agreements mentioned in the mergo README section above
are relevant to that project's source code only.