Due to error handling in Go, I often end up with multiple values functions. So far, the way I have managed this has been very messy and I am looking for best practices to write cleaner code.
Let's say I have the following function:
type Item struct {
Value int
Name string
}
func Get(value int) (Item, error) {
// some code
return item, nil
}
How can I assign a new variable to item.Value
elegantly. Before introducing the error handling, my function just returned item
and I could simply do this:
val := Get(1).Value
Now I do this:
item, _ := Get(1)
val := item.Value
Isn't there a way to access directly the first returned variable?
No, but that is a good thing since you should always handle your errors.
There are techniques that you can employ to defer error handling, see Errors are values by Rob Pike.
In this example from the blog post he illustrates how you could create an
errWriter
type that defers error handling till you are done callingwrite
.In case of a multi-value return function you can't refer to fields or methods of a specific value of the result when calling the function.
And if one of them is an
error
, it's there for a reason (which is the function might fail) and you should not bypass it because if you do, your subsequent code might also fail miserably (e.g. resulting in runtime panic).However there might be situations where you know the code will not fail in any circumstances. In these cases you can provide a helper function (or method) which will discard the
error
(or raise a runtime panic if it still occurs).This can be the case if you provide the input values for a function from code, and you know they work.
Great examples of this are the
template
andregexp
packages: if you provide a valid template or regexp at compile time, you can be sure they can always be parsed without errors at runtime. For this reason thetemplate
package provides theMust(t *Template, err error) *Template
function and theregexp
package provides theMustCompile(str string) *Regexp
function: they don't returnerror
s because their intended use is where the input is guaranteed to be valid.Examples:
Back to your case
IF you can be certain
Get()
will not produceerror
for certain input values, you can create a helperMust()
function which would not return theerror
but raise a runtime panic if it still occurs:But you should not use this in all cases, just when you're sure it succeeds. Usage:
Alternative / Simplification
You can even simplify it further if you incorporate the
Get()
call into your helper function, let's call itMustGet
:Usage:
See some interesting / related questions:
how to parse multiple returns in golang
Return map like 'ok' in Golang on normal functions
No, you cannot directly access the first value.
I suppose a hack for this would be to return an array of values instead of "item" and "err", and then just do
item, _ := Get(1)[0]
but I would not recommend this.How about this way?
Yes, there is.
Surprising, huh? You can get a specific value from a multiple return using a simple
mute
function:https://play.golang.org/p/IwqmoKwVm-
Notice how you select the value number just like you would from a slice/array and then the type to get the actual value.
You can read more about the science behind that from this article. Credits to the author.