4 ms·
One thing I never understood with Go was what's the deal with multiple return values in functions? Why would you do this? What was the reasoning?
by Keyframe 10y ago
One thing I never understood with Go was what's the deal with multiple return values in functions? Why would you do this? What was the reasoning?
- twic 10y agoOne of Go's other design decisions is signalling failures by returning errors. Multiple return values makes it possible to do that and also return a real value. If you didn't have multiple return values, you would have to do something ugly: C-style pseudo-return via a passed-in pointer, or returning an error or a value as an interface{} and requiring the caller to type-switch on it. Or add a special case to the language to somehow allow an error return alongside a normal return. Multiple return makes this straightforward and uniform.
- Keyframe 10y agoAlright, that actually makes sense. Thanks.
- nvarsj 10y agoMy guess is it comes from its C language roots, where every method returns an error code. With multiple return values, you can return an error code and the return value. This is a nice improvement over having to do my_func(in, &out).