Nil slices

The zero value of a slice is nil.

A nil slice has a length and capacity of 0 and has no underlying array.

nil-slices.go

  1. package main
  2. import "fmt"
  3. func main() {
  4. var s []int
  5. fmt.Println(s, len(s), cap(s))
  6. if s == nil {
  7. fmt.Println("nil!")
  8. }
  9. }