Junior — Middle
What value is stored in a slice if it was defined but not explicitly initialized?
sobes.tech AI
Answer from AI
If a slice in Go is declared but not explicitly initialized (for example, var s []int), it has the value nil. Such a slice does not point to any array, and its length and capacity are zero.
This is different from an empty slice, which is created, for example, like this: s := []int{} — it is not equal to nil, but also has a length of 0.
Example:
var s []int
fmt.Println(s == nil) // true
fmt.Println(len(s)) // 0
s = []int{}
fmt.Println(s == nil) // false
fmt.Println(len(s)) // 0
Thus, an uninitialized slice holds the value nil.