Source file tour/moretypes/slice-len-cap.go

     1  //go:build OMIT
     2  
     3  package main
     4  
     5  import "fmt"
     6  
     7  func main() {
     8  	s := []int{2, 3, 5, 7, 11, 13}
     9  	printSlice(s)
    10  
    11  	// Slice the slice to give it zero length.
    12  	s = s[:0]
    13  	printSlice(s)
    14  
    15  	// Extend its length.
    16  	s = s[:4]
    17  	printSlice(s)
    18  
    19  	// Drop its first two values.
    20  	s = s[2:]
    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