Junior — Middle
Can an empty collection be considered equal to the Nil value in programming?
sobes.tech AI
Answer from AI
In Go, an empty collection (for example, an empty slice or map) is not equal to nil. nil indicates the absence of a value or initialization, whereas an empty collection is an initialized object with zero elements.
Example:
var s []int // s == nil
s = []int{} // s != nil, but len(s) == 0
fmt.Println(s == nil) // true for the first case
fmt.Println(len(s) == 0) // true for both cases
Therefore, an empty collection cannot be considered equal to nil, even though it contains no elements.