By Spearfisher


2015-01-30 00:10:16 8 Comments

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?

5 comments

@icza 2015-01-30 09:49:56

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 and regexp 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 the template package provides the Must(t *Template, err error) *Template function and the regexp package provides the MustCompile(str string) *Regexp function: they don't return errors because their intended use is where the input is guaranteed to be valid.

Examples:

// "text" is a valid template, parsing it will not fail
var t = template.Must(template.New("name").Parse("text"))

// `^[a-z]+\[[0-9]+\]$` is a valid regexp, always compiles
var validID = regexp.MustCompile(`^[a-z]+\[[0-9]+\]$`)

Back to your case

IF you can be certain Get() will not produce error for certain input values, you can create a helper Must() function which would not return the error but raise a runtime panic if it still occurs:

func Must(i Item, err error) Item {
    if err != nil {
        panic(err)
    }
    return i
}

But you should not use this in all cases, just when you're sure it succeeds. Usage:

val := Must(Get(1)).Value

Alternative / Simplification

You can even simplify it further if you incorporate the Get() call into your helper function, let's call it MustGet:

func MustGet(value int) Item {
    i, err := Get(value)
    if err != nil {
        panic(err)
    }
    return i
}

Usage:

val := MustGet(1).Value

See some interesting / related questions:

how to parse multiple returns in golang

Return map like 'ok' in Golang on normal functions

@Kesarion 2016-12-21 15:46:57

Yes, there is.

Surprising, huh? You can get a specific value from a multiple return using a simple mute function:

package main

import "fmt"
import "strings"

func µ(a ...interface{}) []interface{} {
    return a
}

type A struct {
    B string
    C func()(string)
}

func main() {
    a := A {
        B:strings.TrimSpace(µ(E())[1].(string)),
        C:µ(G())[0].(func()(string)),
    }

    fmt.Printf ("%s says %s\n", a.B, a.C())
}

func E() (bool, string) {
    return false, "F"
}

func G() (func()(string), bool) {
    return func() string { return "Hello" }, true
}

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.

@Jaehoon 2015-01-30 08:52:07

How about this way?

package main

import (
    "fmt"
    "errors"
)

type Item struct {
    Value int
    Name string
}

var items []Item = []Item{{Value:0, Name:"zero"}, 
                        {Value:1, Name:"one"}, 
                        {Value:2, Name:"two"}}

func main() {
    var err error
    v := Get(3, &err).Value
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(v)

}

func Get(value int, err *error) Item {
    if value > (len(items) - 1) {
        *err = errors.New("error")
        return Item{}
    } else {
        return items[value]
    }
}

@Antimony 2015-01-30 03:51:26

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.

@jmaloney 2015-01-30 00:58:51

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.

ew := &errWriter{w: fd}
ew.write(p0[a:b])
ew.write(p1[c:d])
ew.write(p2[e:f])
// and so on
if ew.err != nil {
    return ew.err
}

In this example from the blog post he illustrates how you could create an errWriter type that defers error handling till you are done calling write.

Related Questions

Sponsored Content

14 Answered Questions

[SOLVED] How to return multiple values from a function?

10 Answered Questions

[SOLVED] Can I try/catch a warning?

  • 2009-08-06 21:51:30
  • user121196
  • 171681 View
  • 307 Score
  • 10 Answer
  • Tags:   php error-handling

21 Answered Questions

[SOLVED] Error handling in C code

  • 2008-12-22 10:46:04
  • Laserallan
  • 81770 View
  • 136 Score
  • 21 Answer
  • Tags:   c error-handling

1 Answered Questions

[SOLVED] Returning error value in reflect.MakeFunc

  • 2016-12-24 01:17:26
  • Ozum Safa
  • 368 View
  • 0 Score
  • 1 Answer
  • Tags:   go reflection

8 Answered Questions

[SOLVED] How to elegantly ignore some return values of a MATLAB function?

1 Answered Questions

[SOLVED] Assign multi-value to struct literal

  • 2015-11-09 09:02:19
  • Aetherix
  • 311 View
  • 0 Score
  • 1 Answer
  • Tags:   go

2 Answered Questions

2 Answered Questions

[SOLVED] Why reflect.ValueOf can not print out true value of int using Println in golang

  • 2014-04-09 14:58:44
  • user3505400
  • 320 View
  • 0 Score
  • 2 Answer
  • Tags:   reflection go

1 Answered Questions

[SOLVED] Multiple-value in single-value context

  • 2015-04-14 14:57:23
  • DenverCoder21
  • 1664 View
  • 2 Score
  • 1 Answer
  • Tags:   go

Sponsored Content