2019-10-21 04:22:28 -04:00
---
2023-10-20 03:42:39 -04:00
title: collections.Merge
description: Returns the result of merging two or more maps.
2023-12-04 09:14:18 -05:00
categories: []
2023-10-20 03:42:39 -04:00
keywords: []
2023-12-04 09:14:18 -05:00
action:
2023-10-20 03:42:39 -04:00
aliases: [merge]
2023-12-04 09:14:18 -05:00
related:
- functions/collections/Append
2023-10-20 03:42:39 -04:00
returnType: any
signatures: [collections.Merge MAP MAP...]
aliases: [/functions/merge]
2019-10-21 04:22:28 -04:00
---
2022-02-28 04:37:27 -05:00
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.
2020-10-06 10:22:20 -04:00
Key handling is case-insensitive.
2022-02-28 04:37:27 -05:00
The following examples use these map definitions:
2019-10-21 04:22:28 -04:00
```go-html-template
2022-02-28 04:37:27 -05:00
{{ $m1 := dict "x" "foo" }}
{{ $m2 := dict "x" "bar" "y" "wibble" }}
{{ $m3 := dict "x" "baz" "y" "wobble" "z" (dict "a" "huey") }}
2019-10-21 04:22:28 -04:00
```
2022-02-28 04:37:27 -05:00
Example 1
```go-html-template
{{ $merged := merge $m1 $m2 $m3 }}
2019-10-21 04:22:28 -04:00
2023-08-07 04:35:12 -04:00
{{ $merged.x }} → baz
{{ $merged.y }} → wobble
{{ $merged.z.a }} → huey
2019-10-21 04:22:28 -04:00
```
2022-02-28 04:37:27 -05:00
Example 2
```go-html-template
{{ $merged := merge $m3 $m2 $m1 }}
2023-08-07 04:35:12 -04:00
{{ $merged.x }} → foo
{{ $merged.y }} → wibble
{{ $merged.z.a }} → huey
2022-02-28 04:37:27 -05:00
```
Example 3
```go-html-template
{{ $merged := merge $m2 $m3 $m1 }}
2023-08-07 04:35:12 -04:00
{{ $merged.x }} → foo
{{ $merged.y }} → wobble
{{ $merged.z.a }} → huey
2022-02-28 04:37:27 -05:00
```
Example 4
```go-html-template
{{ $merged := merge $m1 $m3 $m2 }}
2023-08-07 04:35:12 -04:00
{{ $merged.x }} → bar
{{ $merged.y }} → wibble
{{ $merged.z.a }} → huey
2019-10-21 04:22:28 -04:00
```
{{% note %}}
2023-10-20 03:42:39 -04:00
Regardless of depth, merging only applies to maps. For slices, use [append ](/functions/collections/append ).
2019-10-21 04:22:28 -04:00
{{% /note %}}