What Is a Nil Slice vs an Empty Slice in Go

A nil slice has no underlying array, whereas an empty slice has an underlying array but zero length.

A nil slice has no underlying array and a length of 0, while an empty slice has an underlying array but a length of 0. Use nil for uninitialized data and []T{} for an initialized but empty collection.

var nilSlice []int          // len=0, cap=0, slice is nil
emptySlice := []int{}       // len=0, cap=0, slice is not nil
emptySlice2 := make([]int, 0) // len=0, cap=0, slice is not nil

if nilSlice == nil {
    fmt.Println("nilSlice is nil")
}
if emptySlice == nil {
    fmt.Println("emptySlice is nil") // This will not print
}