Go
map Operations
CRUD operations on map.
By EZ4Code Team
mapdict
Code
package main
import "fmt"
func main() {
m := make(map[string]int)
m["a"] = 1
m["b"] = 2
// Read (check existence)
v, ok := m["a"]
if ok {
fmt.Println(v)
}
// Delete
delete(m, "b")
// Iterate
for k, v := range m {
fmt.Printf("%s=%d\n", k, v)
}
// Literal
m2 := map[string][]int{
"even": {2, 4, 6},
"odd": {1, 3, 5},
}
fmt.Println(m2)
}Explanation
map is an unordered key-value pair; check key existence via the ok idiom.