Go has very neat multiple return values paradigm. But It looks like v, ok := map[key]
and v, k := range m
use different mechanism with same notation. Here is a simple example:
func f2() (k, v string) {
return "Hello", "World"
}
func main(){
k := f2() // Doesn't work : multiple-value f2() in single-value context
m := map[string]int{"One": 1}
// It works
v, ok := m["One"]
// How it all work?
v := m["One"]
for k := range m {}
}
In above example, k := f2()
gives error as f2
returns two values, whereas v, ok := m["One"]
and v := m["One"]
- both expressions work without any error.
Why is that different behavior?