Sobes.tech
Junior — Middle

Kas võib pidada tühja kogumi Nil väärtusega võrdseks programmeerimises?

sobes.tech AI

Vastus AI-lt

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 if it contains no elements.