`I do find this rather annoying, to the point where I'll stick a "var err 
error" at the top of a function rather than have the first assignment be 
different to the subsequent ones. `

I think this is a good way. I usually code in this way if there're multiple 
error assignments.

If each error assignment statement only return single return value (the 
error itself), I code it in one-liner way.

On Friday, November 12, 2021 at 4:53:21 PM UTC+8 Kn wrote:

>
> Oh, I have a typo error, in the second pattern, `err := doAnotherthing()` 
> should be `err = doAnotherthing()`.
> On Friday, November 12, 2021 at 4:02:34 PM UTC+8 Brian Candler wrote:
>
>> func MyFunc() error {
>>   v, err := doSomething()
>>   ...
>>   err := doAnotherthing()
>> }
>>
>> That won't compile anyway; the second assignment would have to be
>>
>>   err = doAnotherthing()
>>
>> See: https://play.golang.org/p/pvNC7YHI88j
>>
>> I do find this rather annoying, to the point where I'll stick a "var err 
>> error" at the top of a function rather than have the first assignment be 
>> different to the subsequent ones.  And yet, a single assignment is fine if 
>> you use the assign-and-test form, since this introduces a new variable 
>> which is only scoped to that block:
>>
>>         // This is OK
>> if err := doAnotherthing(); err != nil {
>> return err
>> }
>>
>> See: https://play.golang.org/p/4NKmFoX7uj2
>>
>> It is also possible to use named return values and a naked return, 
>> although I get the impression this tends to be frowned upon.
>>
>

-- 
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/714a465e-1581-4c45-ab70-51a9bef4cef2n%40googlegroups.com.

Reply via email to