Junior — Middle
Kas Go vaadetes omavad sarnaseid pikkuse ja mahutavuse meetodeid nagu viiludega?
sobes.tech AI
Vastus AI-lt
In Go, maps do not have length and capacity methods like slices. To get the length, the built-in len() function is used, which returns the number of elements in the map.
For slices, len() can be used for the current length and cap() for the capacity (maximum size without allocating new memory). In maps, cap() does not exist because they grow dynamically as elements are added.
Example:
m := make(map[string]int)
m["a"] = 1
fmt.Println(len(m)) // prints 1
s := make([]int, 0, 10)
fmt.Println(len(s)) // 0
fmt.Println(cap(s)) // 10