Equivalent of setdefault in Go?
go, python
Solution
You can always define it yourself:
func setDefault(h map[string]int, k string, v int) (set bool, r int) {
if r, set = h[k]; !set {
h[k] = v
r = v
set = true
}
return
}
But no, it's not in the stdlib. Usually, you'd just do this inline.
Problem
I can do: ``` _, ok := some_go_map[a_key] ``` to test for existence of key. But I've been spoiled by Python's dict's `setdefault` method (if a key does not have value set in a "map" [dict == associative array], set it to a given default, then get it; otherwise just get). Wondering if there's some idiom in Go to achieve the same thing?