attended := map[string]bool{"Ann": true,"Joe": true,...}
if attended[person] { // will be false if person is not in the mapfmt.Println(person, "was at the meeting")}
var empty struct{}var ok boolvar m map[string]struct{}m = make(map[string]struct{})m["somestring"] = empty
_, ok = m["somestring"]fmt.Println("somestring exists?", ok)_, ok = m["not"]fmt.Println("not exists?", ok)
m := map[int]string{}s := m[1] // s will be the empty string ""var m2 map[int]float64 // m2 is nil!f := m2[2] // f will be 0.0
fmt.Printf("%q %f", s, f) // Prints: "" 0.000000
set := map[string]bool{"one": true,"two": true,}
fmt.Println("Contains 'one':", set["one"])
if set["two"] {fmt.Println("'two' is in the set")}if !set["three"] {fmt.Println("'three' is not in the set")}
package main
import ("fmt")
func main() {//creating a map with 3 key-value pairssampleMap := map[string]int{"key1": 100, "key2": 500, "key3": 999}//A two value assignment can be used to check existence of a key.value, isKeyPresent := sampleMap["key2"]//isKeyPresent will be true if key present in sampleMapif isKeyPresent {//key existfmt.Println("key present, value = ", value)} else {//key does not existfmt.Println("key does not exist")}}
func findPairs(slice1 []int, sum int) {pairMap := make(map[int]int)for i, v := range slice1 {if valuei, ok := pairMap[v]; ok {fmt.Println("Pair Found", i, valuei)} else {pairMap[sum-v] = i}}}