Menu

Toshimaru's Blog

[Go]Append item to slice

append Usage

func append(slice []Type, elems ...Type) []Type

The append built-in function appends elements to the end of a slice.

builtin package - builtin - pkg.go.dev

Example

Add a item to slice

var s []int
s = append(s, 1)
fmt.Println(s) // [1]

Add multiple items to slice

var s []int
s = append(s, 1)
fmt.Println(s) // [1 2 3 4 5]

Add another slice to slice

var s []int
s2 := []int{3, 4, 5}
s = append(s, s2...)
fmt.Println(s) // [3 4 5]

See also

Load more