Dear gonuts-group,
I'd like to know if it would be somehow possible to call a function in an
assignment with the value to assign as argument.
That could improve automatic error handling very much, here's how I imagine
money, HandleError() := ioutil.ReadFile(file)
with HandleError() being a function to react based on the error's value.
If that turns out to be possible, I'd be more than surprised, but I thought
it wouldn't be too much to ask.

Have a good day


Search Discussions

  • Andrew Gerrand at Oct 30, 2012 at 2:03 pm
    No, it's not possible.


  • Carlos Castillo at Oct 30, 2012 at 9:11 pm
    Take a look at http://golang.org/pkg/text/template/#Must, and its code.

    It essentially does what I think you are looking for: act on one return
    value (the error) and pass back the other to the caller in the case of no
    error. It is slightly more awkward than your code, but has the benefit that
    it's valid Go. ;-)

    Be careful with this "pattern" though, as it is admitted in the
    template.Must documentation, it has a very limited set of suggested use
    cases, and trying to shoehorn it into more situations where errors occur
    will probably result in more confusion down the line. For me, in most
    situations when I need to handle errors, the one or two extra lines need
    for an if-statement provides far greater control over the situation, and is
    much more readable for myself and others in the future.

    Thank you, however, for the polite and humble *question.*

    I will try to have a nice day, and hope the same for you.


Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
postedOct 30, '12 at 1:29p
activeOct 30, '12 at 9:11p



site design / logo © 2022 Grokbase