The append()
function in Go appends the specified elements to the end of a slice.
The
append()
function returns the updated slice of the same type as the input slice.
package mainimport "fmt"func main(){// Example 1// create a slice of type intexample := []int{11,22,33,44}// display slicefmt.Println(example)// append 55example = append(example, 55)//display slicefmt.Println(example)// Example 2// append 66 and 77example = append(example, 66, 77)//display slicefmt.Println(example)// Example 3anotherSlice := []int{88,99,111}example = append(example, anotherSlice...)fmt.Println(example)}
The program creates a slice of type int
that contains 11, 22, 33, 44.
Free Resources