Golang ProgramsGolang Programs

How to add and update elements in Map?

The built-in len() function returns the number of elements in a map.
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
}
C:\golang\maps>go run example1.go
map[Mark:10 Sandy:20]
map[Mark:50 Sandy:20 Rocky:30 Josef:40]

C:\golang\maps>
2 elements added and 1 edited in employee map after initialization.