Sobes.tech
Junior — Middle

How to determine the current size of a variable-length array that has not yet been initialized?

sobes.tech AI

Answer from AI

In Go, if you have a slice with variable length that has not been initialized, its length will be 0.

For example:

var arr []int // uninitialized slice
fmt.Println(len(arr)) // will output 0

Thus, to determine the current size of the slice, simply call the len() function:

length := len(arr)

If the slice is uninitialized (nil), len() will return 0, which allows safe operation without additional checks.