Built-in function
Go append()
Adds elements to a slice and returns the resulting slice. Store the returned value: if capacity is insufficient, a new backing array is allocated; otherwise the existing array is reused. A byte slice also accepts a string followed by .... Example prints [2 4 6].
Formal Syntax & Structure
go syntaxgo
append(s, element1, element2) | append(s, otherSlice...) | append(byteSlice, stringValue...)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| s | slice | Destination slice. |
| values | element values or expanded compatible slice | Zero or more elements. |
Return Value
same slice type
Slice with the appended elements.
Working Example
Example Snippetgo
package main
import "fmt"
func main() { s := []int{2, 4} s = append(s, 6) fmt.Println(s)}