merge.md (1538B)
1 --- 2 title: merge 3 description: "Returns the result of merging two or more maps." 4 date: 2019-08-08 5 categories: [functions] 6 menu: 7 docs: 8 parent: "functions" 9 keywords: [dictionary] 10 signature: ["collections.Merge MAP MAP...", "merge MAP MAP..."] 11 workson: [] 12 hugoversion: "0.56.0" 13 relatedfuncs: [dict, append, reflect.IsMap, reflect.IsSlice] 14 aliases: [] 15 --- 16 17 Returns the result of merging two or more maps from left to right. If a key already exists, `merge` updates its value. If a key is absent, `merge` inserts the value under the new key. 18 19 Key handling is case-insensitive. 20 21 The following examples use these map definitions: 22 23 ```go-html-template 24 {{ $m1 := dict "x" "foo" }} 25 {{ $m2 := dict "x" "bar" "y" "wibble" }} 26 {{ $m3 := dict "x" "baz" "y" "wobble" "z" (dict "a" "huey") }} 27 ``` 28 29 Example 1 30 31 ```go-html-template 32 {{ $merged := merge $m1 $m2 $m3 }} 33 34 {{ $merged.x }} --> baz 35 {{ $merged.y }} --> wobble 36 {{ $merged.z.a }} --> huey 37 ``` 38 39 Example 2 40 41 ```go-html-template 42 {{ $merged := merge $m3 $m2 $m1 }} 43 44 {{ $merged.x }} --> foo 45 {{ $merged.y }} --> wibble 46 {{ $merged.z.a }} --> huey 47 ``` 48 49 Example 3 50 51 ```go-html-template 52 {{ $merged := merge $m2 $m3 $m1 }} 53 54 {{ $merged.x }} --> foo 55 {{ $merged.y }} --> wobble 56 {{ $merged.z.a }} --> huey 57 ``` 58 59 Example 4 60 61 ```go-html-template 62 {{ $merged := merge $m1 $m3 $m2 }} 63 64 {{ $merged.x }} --> bar 65 {{ $merged.y }} --> wibble 66 {{ $merged.z.a }} --> huey 67 ``` 68 69 {{% note %}} 70 Regardless of depth, merging only applies to maps. For slices, use [append]({{< ref "functions/append" >}}). 71 {{% /note %}}