Slice defaults

When slicing, you may omit the high or low bounds to use their defaults instead.

The default is zero for the low bound and the length of the slice for the high bound.

For the array

  1. var a [10]int

these slice expressions are equivalent:

  1. a[0:10]
  2. a[:10]
  3. a[0:]
  4. a[:]

slice-bounds.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. s := []int{2, 3, 5, 7, 11, 13}
  5. s = s[1:4]
  6. fmt.Println(s)
  7. s = s[:2]
  8. fmt.Println(s)
  9. s = s[1:]
  10. fmt.Println(s)
  11. }