Sobes.tech
Junior — Middle

What does an uninitialized slice contain in a programming language?

sobes.tech AI

Answer from AI

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

Uninitialized slice:

  • Pointer is nil.
  • 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 via append, after which the slice will be automatically initialized.

What does an uninitialized slice contain in a… - sobes.tech