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]
2 elements added and 1 edited in employee map after initialization.
Most Helpful This Week