Sobes.tech
Junior — Middle

Kas on võimalik teha andmete lugemise operatsiooni sellisest lõigust, mis pole eelnevalt initsialiseeritud?

sobes.tech AI

Vastus AI-lt

In Go, slices are structures that contain a pointer to an array, a length, and a capacity. If a slice is not initialized (i.e., it is nil), attempting to read from it will cause a panic.

For example:

var s []int // nil-slice
fmt.Println(s[0]) // panic: runtime error: index out of range

To safely read from a slice, it must be initialized, for example, using make or by assigning an existing array:

s := make([]int, 3)
fmt.Println(s[0]) // 0, safe

Therefore, reading from an uninitialized slice is not possible — it will cause a runtime error.