Go Slices append

Introduction

Go slices append() adds elements to the end of a slice.

If there's sufficient capacity in the underlying array, the element is placed after the last element and the length is incremented.

If there is not sufficient capacity, a new array is created, all of the existing elements are copied over.

Here is an example:


package main/* ww w.j  av  a 2 s . c o  m*/

import "fmt" 

func main() { 
    slice1  := []int{1,2,3} 
    slice2  := append(slice1, 4, 5) 
    fmt.Println(slice1, slice2) 
} 



PreviousNext

Related