Golang builtin.append() function example

package builtin

The append built-in function appends elements to the end of a slice. If it has sufficient capacity, the destination is resliced to accommodate the new elements. If it does not, a new underlying array will be allocated. Append returns the updated slice. It is therefore necessary to store the result of append, often in the variable holding the slice itself:

Golang builtin. append() function usage example

 package main

 import "fmt"

 func main() {

 slice1 := []string(nil)

 fmt.Println("slice1 has elements of ", slice1)

 slice1 = append(slice1, "a")

 fmt.Println("After append, now slice1 has elements of ", slice1)

 }

Output :

slice1 has elements of []

After append, now slice1 has elements of [a]

Reference :

http://golang.org/pkg/builtin/#append

Advertisement