Project Name | Stars | Downloads | Repos Using This | Packages Using This | Most Recent Commit | Total Releases | Latest Release | Open Issues | License | Language |
---|---|---|---|---|---|---|---|---|---|---|
Mergo | 2,403 | 7,285 | 7,113 | 4 days ago | 15 | May 25, 2022 | 33 | bsd-3-clause | Go | |
Mergo: merging Go structs and maps since 2013 | ||||||||||
Gormt | 2,183 | 3 | 7 days ago | 20 | April 14, 2021 | 55 | mit | Go | ||
database to golang struct | ||||||||||
Toml | 288 | 341 | 497 | 10 months ago | 6 | July 30, 2021 | 6 | mit | Go | |
TOML parser and encoder library for Golang | ||||||||||
Structable | 191 | 1 | 5 years ago | August 12, 2014 | 3 | other | Go | |||
Golang struct-to-table database mapper | ||||||||||
Go Gateway | 82 | 7 years ago | June 04, 2021 | 5 | Go | |||||
Use https://github.com/apex/up | ||||||||||
Gossie | 82 | 8 years ago | May 24, 2021 | 4 | Go | |||||
Cassandra library for Go | ||||||||||
Tablestruct | 60 | 9 years ago | 1 | July 20, 2014 | apache-2.0 | Go | ||||
tablestruct creates mappings between Go structs and database tables. | ||||||||||
Jsonutilities | 53 | 3 years ago | 17 | September 28, 2019 | 6 | mit | Swift | |||
Easily load JSON objects and decode them into structs or classes | ||||||||||
Mappingace | 50 | 6 years ago | 5 | October 22, 2018 | mit | Swift | ||||
[Swift JSON to Struct, class] MappingAce allows rapid creation of swift struct , Swift class, OC class . Automatic transform dictionary to model(model could be struct), forget to manually write property mapping code | ||||||||||
Binstruct | 47 | 2 | a year ago | 8 | April 24, 2022 | mit | Go | |||
Golang binary decoder for mapping data into the structure |
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.
It is ready for production use. It is used in several projects by Docker, Google, The Linux Foundation, VMWare, Shopify, Microsoft, etc.
Please keep in mind that a problematic PR broke 0.3.9. I reverted it in 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, 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).
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. 😍
go get github.com/imdario/mergo
// use in your .go code
import (
"github.com/imdario/mergo"
)
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 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 }
}
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.