package mergo
Import Path
github.com/imdario/mergo (on go.dev)
Dependency Relation
imports 6 packages, and imported by one package
Involved Source Files
Package mergo merges same-type structs and maps by setting default values in zero-value fields.
Mergo won't merge unexported (private) fields but will do recursively any exported one. It also won't merge structs inside maps (because they are not addressable using Go reflection).
Usage
From my own work-in-progress project:
type networkConfig struct {
Protocol string
Address string
ServerType string `json: "server_type"`
Port uint16
}
type FssnConfig struct {
Network networkConfig
}
var fssnDefault = FssnConfig {
networkConfig {
"tcp",
"127.0.0.1",
"http",
31560,
},
}
// Inside a function [...]
if err := mergo.Merge(&config, fssnDefault); err != nil {
log.Fatal(err)
}
// More code [...]
map.go
merge.go
mergo.go
Package-Level Type Names (total 3, in which 2 are exported)
AppendSlice bool
Overwrite bool
Transformers Transformers
TypeCheck bool
overwriteSliceWithEmptyValue bool
overwriteWithEmptyValue bool
func WithAppendSlice(config *Config)
func WithOverride(config *Config)
func WithOverrideEmptySlice(config *Config)
func WithOverwriteWithEmptyValue(config *Config)
func WithTypeCheck(config *Config)
func deepMap(dst, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (err error)
func deepMerge(dstIn, src reflect.Value, visited map[uintptr]*visit, depth int, config *Config) (dst reflect.Value, err error)
( T) Transformer(reflect.Type) func(dst, src reflect.Value) error
func WithTransformers(transformers Transformers) func(*Config)
Package-Level Functions (total 22, in which 10 are exported)
Map sets fields' values in dst from src.
src can be a map with string keys or a struct. dst must be the opposite:
if src is a map, dst must be a valid pointer to struct. If src is a struct,
dst must be map[string]interface{}.
It won't merge unexported (private) fields and will do recursively
any exported field.
If dst is a map, keys will be src fields' names in lower camel case.
Missing key in src that doesn't match a field in dst will be skipped. This
doesn't apply if dst is a map.
This is separated method from Merge because it is cleaner and it keeps sane
semantics: merging equal types, mapping different (restricted) types.
MapWithOverwrite will do the same as Map except that non-empty dst attributes will be overridden by
non-empty src attribute values.
Deprecated: Use Map(…) with WithOverride
Merge will fill any empty for value type attributes on the dst struct using corresponding
src attributes if they themselves are not empty. dst and src must be valid same-type structs
and dst must be a pointer to struct.
It won't merge unexported (private) fields and will do recursively any exported field.
MergeWithOverwrite will do the same as Merge except that non-empty dst attributes will be overridden by
non-empty src attribute values.
Deprecated: use Merge(…) with WithOverride
WithAppendSlice will make merge append slices instead of overwriting it.
WithOverride will make merge override non-empty dst attributes with non-empty src attributes values.
WithOverrideEmptySlice will make merge override empty dst slice with empty src slice.
WithOverwriteWithEmptyValue will make merge override non empty dst attributes with empty src attributes values.
WithTransformers adds transformers to merge, allowing to customize the merging of some types.
WithTypeCheck will make merge check types while overwriting it (must be used with WithOverride).
Package-Level Variables (total 5, all are exported)
Errors reported by Mergo when it finds invalid arguments.
Errors reported by Mergo when it finds invalid arguments.
Errors reported by Mergo when it finds invalid arguments.
Errors reported by Mergo when it finds invalid arguments.
Errors reported by Mergo when it finds invalid arguments.
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |