How to add and update elements in Map?
The built-in
len()
function returns the number of elements in a map.
Example
package main
import "fmt"
func main() {
var employee = make(map[string]int)
employee["Mark"] = 10
employee["Sandy"] = 20
// Empty Map
employeeList := make(map[string]int)
fmt.Println(len(employee)) // 2
fmt.Println(len(employeeList)) // 0
}
Output
map[Mark:10 Sandy:20]
map[Mark:50 Sandy:20 Rocky:30 Josef:40]
employee
map after initialization.
Most Helpful This Week
How to remove symbols from a string in Golang?
Split a string at uppercase letters using regular expression in Golang
Different ways for Integer to String Conversions
How to iterate over a Map using for loop in Go?
Example to create custom error
How to replace emoji characters in string using regex in Golang?