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/f3251f3b-fe94-4dba-badb-c422f96dc666n%40googlegroups.com.