Sobes.tech
Junior — Middle

Do maps in Go have the same length and capacity methods as slices?

sobes.tech AI

Answer from AI

In Go, maps do not have length and capacity methods like slices (slices). Instead, the built-in function len() is used to get the number of elements in a map.

For slices, len() can be used for the current length and cap() for capacity (the maximum size without allocating new memory). Maps do not have cap() because they grow dynamically as elements are added.

Example:

m := make(map[string]int)
m["a"] = 1
fmt.Println(len(m)) // outputs 1

s := make([]int, 0, 10)
fmt.Println(len(s)) // 0
fmt.Println(cap(s)) // 10
Do maps in Go have the same length and capacity… - sobes.tech