3 ms·
The difference in behavior between the one-return-value and the two-return-value version is that the former returns a zero value when the key does not exist in
by majewsky 9y ago
The difference in behavior between the one-return-value and the two-return-value version is that the former returns a zero value when the key does not exist in the collection. The one-return-value version is only there, basically, because you can use its return value inside an expression, whereas multiple return values have to be unpacked into variables first (unless you `return` them directly).
If Go had generics, the overloads could be merged into a single operator[] that returns a Maybe<ValueType>. Then that Maybe type would have an Or() method to supply a default, e.g.
doSomethingWith(collection["foo"].Or(42))
doSomethingWith(collection["foo"].OrZero())