Source file tour/moretypes/append.go

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import "fmt"
     6  
     7  func main() {
     8  	var s []int
     9  	printSlice(s)
    10  
    11  	// append works on nil slices.
    12  	s = append(s, 0)
    13  	printSlice(s)
    14  
    15  	// The slice grows as needed.
    16  	s = append(s, 1)
    17  	printSlice(s)
    18  
    19  	// We can add more than one element at a time.
    20  	s = append(s, 2, 3, 4)
    21  	printSlice(s)
    22  }
    23  
    24  func printSlice(s []int) {
    25  	fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
    26  }
    27  

View as plain text