Sobes.tech
Junior — Middle

Mida sisaldab programmeerimiskeeles initsialiseerimata lõik?

sobes.tech AI

Vastus AI-lt

In Go, a slice is a structure that contains three fields: a pointer to an array, a length (len), and a capacity (cap). If the slice is not initialized, that is, declared but not assigned, it has the zero value nil.

Uninitialized slice:

  • The pointer is equal to nil.
  • The length and capacity are 0.

Example:

var s []int
fmt.Println(s == nil) // true
fmt.Println(len(s))    // 0
fmt.Println(cap(s))    // 0

Such a slice does not point to any array and contains no elements. It can be safely used in operations, for example, adding elements with append, after which the slice will be initialized automatically.