Sobes.tech
Junior — Middle

Millised sisseehitatud funktsioonid ja toimingud on lubatud veel initsialiseerimata lõigule?

sobes.tech AI

Vastus AI-lt

In Go, a slice that has not been initialized (i.e., is nil) can be used for the following operations:

  • Nil check: if slice == nil {}
  • Getting length and capacity: len(slice) and cap(slice) will return 0
  • Adding elements using append, for example:
var s []int // nil-slice
s = append(s, 1, 2, 3) // now the slice is initialized and contains elements

However, you cannot access elements by index, for example s[0], if the slice is nil or empty — this will cause a panic.