(Previous post deleted to to premature "Post")

My 2 cents. I'm a seasoned Go programmer with years of professional Go 
experience, and decades of coding work. 

Personally, I would avoid goto if possible. It may "have been a best 
practice for a long time" in C/C++ and other languages. But in Go, I find 
there are usually better ways to write such code. The are situations where 
a goto for exit cleanup is the clearest solution, but I recently worked on 
a 50k line group project where we only did it once. 

Another, a personal preference - I dislike bare returns in *most *situations. 
I know I am not alone in this opinion. Specifying the return values 
explicitly is just clearer, avoids a class of bugs, and can make reading 
the code quicker and easier. 

func ret2(b bool) (int, int) {
     if b {
        return 1, 2
    }
    return 2, 3
}
Or 
func ret2(b bool) (x, y int) {
    x = 1
    y = 2
    if b {
        return x, y
    }
    return 2, 3
}
Of course, the second one only makes sense if there are multiple returns 
that use the "defaults". 

When writing Go code, I think it is always good practice to consider 
readability. Locality is a big part of that. With a bare return there is 
fundamental information "missing" at the point of the return. 


On Monday, July 22, 2019 at 2:51:43 PM UTC-4, Tong Sun wrote:
>
> I want to define a default set of values for the multiple-values-returning 
> function, so when doing error checking along the way, I can return such 
> default set when things go wrong.
>
> How can I do that? 
> Is it possible to make the 17&18 line of the following working?
> https://play.golang.org/p/OX6QwWXc2ch
>
> thx
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/167c5b5e-a084-4deb-a299-93e84ca660af%40googlegroups.com.

Reply via email to