Slice literals

A slice literal is like an array literal without the length.

This is an array literal:

  1. [3]bool{true, true, false}

And this creates the same array as above, then builds a slice that references it:

  1. []bool{true, true, false}

slice-literals.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. q := []int{2, 3, 5, 7, 11, 13}
  5. fmt.Println(q)
  6. r := []bool{true, false, true, true, false, true}
  7. fmt.Println(r)
  8. s := []struct {
  9. i int
  10. b bool
  11. }{
  12. {2, true},
  13. {3, false},
  14. {5, true},
  15. {7, true},
  16. {11, false},
  17. {13, true},
  18. }
  19. fmt.Println(s)
  20. }