Go: Check if a map contains a key
m := map[string]float64{"pi": 3.1416}
v1 := m["pi"] // v1 == 3.1416
v2 := m["foo"] // v2 == 0.0 (zero value)
_, exists := m["pi"] // exists == true
if x, ok := m["pi"]; ok {
fmt.Println(x) // 3.1416
}
Comments
Be the first to comment!