Create map

// define map
var m1 map[int]string

// instantiate map
m1 = make(map[int]string)

// implict map instantiation
m2 := make(map[int]string)

// map literal
m3 := map[int]string{
    1:  "One",
    9:  "Nine"
}

Add item to map

m := map[int]string{
    1:  "One",
    9:  "Nine"
}
m[10] = "Ten"

// m contains 1, 9, 10

Lookup item from map

m := map[int]string{
    1:  "One",
    9:  "Nine"
}
item, ok := m[9]   //item is "Nine; ok is true if the
                    //key was found, false otherwise

Delete item from map

m := map[int]string{
    1:  "One",
    9:  "Nine"
}
delete(m, 9)
//m contains only the `1` item

Delete all content from a map

Assign to the variable pointing to the map a new map:

m := map[int]string{
    1:  "One",
    9:  "Nine"
}
m = make(map[int]string) // m is an initialized, empty map

Count the items in a map

m := map[int]string{
    1:  "One",
    9:  "Nine"
}
l := len(m) // l == 2

Want to hire me?

I'm currently considering remote job opportunities.

I'm interested in Go and/or JavaScript senior software engineer positions.

Read more about me and if you're interested, get in touch.