Maps in Go

Maps

Maps are a convenient and powerful built-in associative data structure in Golang that associate values of one type (the key) with values of another type (the element or value)

Maps declaration and initialization


    //syntax
    m := make(map[key-type]val-type)

    //declaration
    m := make(map[string]int) // key-value string and valuetype int

    // declaration and initialization
    var timeZone = map[string]int{
        "UTC":  0*60*60,
        "EST": -5*60*60,
        "CST": -6*60*60,
        "MST": -7*60*60,
        "PST": -8*60*60,
    }

Modify maps (key/value)

Set key/value pairs using typical name[key] = val syntax

    //get value 
    UTC := timeZone["UTC"]

    //set value
    timeZone["UTC"] = -7*60*60

    //delete key/value
    delete(timeZone, "UTC")

Previous Article

Next Article