Sobes.tech
Junior — Middle

Kas on võimalik kasutada tsüklit nil slice elementide läbimiseks?

sobes.tech AI

Vastus AI-lt

Yes, in Go, you can use a loop to iterate over elements of a nil slice. In this case, the loop simply will not execute because the length of a nil slice is zero.

Example:

var s []int // nil slice
for i, v := range s {
    fmt.Println(i, v) // This code will not execute
}

Thus, iterating over a nil slice is safe and does not cause errors, simplifying working with slices without the need for additional nil checks.