package main
import (
"fmt"
"unsafe"
)
func main() {
var x = []int{}
var y []int
fmt.Println(x == nil) // why it is not nil
fmt.Println(y == nil)
fmt.Println(unsafe.Sizeof(x),unsafe.Sizeof(y))
fmt.Printf("%T",x)
fmt.Printf("%T",y)
}
X is not nil beacuse you are initializing it to a en empty slice.
In the secornd case, y is nil because it is only declared and Go initializes it to nil.
A slice is a structure to a array, with capacity y length its default zero value isnil . The nil slice has a length and capacity of 0 and has no underlying array. When we create a slice with the make function, all elements are initialized to 0.
1 Like