| 1 |
--- |
| 2 |
title: collections.Append |
| 3 |
description: Returns a slice by adding one or more elements, or an entire second slice, to the end of the given slice. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [append] |
| 9 |
returnType: '[]any' |
| 10 |
signatures: |
| 11 |
- collections.Append ELEMENT [ELEMENT...] SLICE |
| 12 |
- collections.Append SLICE1 SLICE2 |
| 13 |
aliases: [/functions/append] |
| 14 |
--- |
| 15 |
|
| 16 |
This function appends all elements, excluding the last, to the last element. This allows [pipe](g) constructs as shown below. |
| 17 |
|
| 18 |
Append a single element to a slice: |
| 19 |
|
| 20 |
```go-html-template |
| 21 |
{{ $s := slice "a" "b" }} |
| 22 |
{{ $s }} → [a b] |
| 23 |
|
| 24 |
{{ $s = $s | append "c" }} |
| 25 |
{{ $s }} → [a b c] |
| 26 |
``` |
| 27 |
|
| 28 |
Append two elements to a slice: |
| 29 |
|
| 30 |
```go-html-template |
| 31 |
{{ $s := slice "a" "b" }} |
| 32 |
{{ $s }} → [a b] |
| 33 |
|
| 34 |
{{ $s = $s | append "c" "d" }} |
| 35 |
{{ $s }} → [a b c d] |
| 36 |
``` |
| 37 |
|
| 38 |
Append two elements, as a slice, to a slice. This produces the same result as the previous example: |
| 39 |
|
| 40 |
```go-html-template |
| 41 |
{{ $s := slice "a" "b" }} |
| 42 |
{{ $s }} → [a b] |
| 43 |
|
| 44 |
{{ $s = $s | append (slice "c" "d") }} |
| 45 |
{{ $s }} → [a b c d] |
| 46 |
``` |
| 47 |
|
| 48 |
Start with an empty slice: |
| 49 |
|
| 50 |
```go-html-template |
| 51 |
{{ $s := slice }} |
| 52 |
{{ $s }} → [] |
| 53 |
|
| 54 |
{{ $s = $s | append "a" }} |
| 55 |
{{ $s }} → [a] |
| 56 |
|
| 57 |
{{ $s = $s | append "b" "c" }} |
| 58 |
{{ $s }} → [a b c] |
| 59 |
|
| 60 |
{{ $s = $s | append (slice "d" "e") }} |
| 61 |
{{ $s }} → [a b c d e] |
| 62 |
``` |
| 63 |
|
| 64 |
If you start with a slice of a slice: |
| 65 |
|
| 66 |
```go-html-template |
| 67 |
{{ $s := slice (slice "a" "b") }} |
| 68 |
{{ $s }} → [[a b]] |
| 69 |
|
| 70 |
{{ $s = $s | append (slice "c" "d") }} |
| 71 |
{{ $s }} → [[a b] [c d]] |
| 72 |
``` |
| 73 |
|
| 74 |
To create a slice of slices, starting with an empty slice: |
| 75 |
|
| 76 |
```go-html-template |
| 77 |
{{ $s := slice }} |
| 78 |
{{ $s }} → [] |
| 79 |
|
| 80 |
{{ $s = $s | append (slice (slice "a" "b")) }} |
| 81 |
{{ $s }} → [[a b]] |
| 82 |
|
| 83 |
{{ $s = $s | append (slice "c" "d") }} |
| 84 |
{{ $s }} → [[a b] [c d]] |
| 85 |
``` |
| 86 |
|
| 87 |
Although the elements in the examples above are strings, you can use the `append` function with any data type, including Pages. For example, on the home page of a corporate site, to display links to the two most recent press releases followed by links to the four most recent articles: |
| 88 |
|
| 89 |
```go-html-template |
| 90 |
{{ $p := where site.RegularPages "Type" "press-releases" | first 2 }} |
| 91 |
{{ $p = $p | append (where site.RegularPages "Type" "articles" | first 4) }} |
| 92 |
|
| 93 |
{{ with $p }} |
| 94 |
<ul> |
| 95 |
{{ range . }} |
| 96 |
<li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li> |
| 97 |
{{ end }} |
| 98 |
</ul> |
| 99 |
{{ end }} |
| 100 |
``` |