DEV Community

Discussion on: My Favorite Go 2 Proposals

Collapse
 
davidmz profile image
Давид Мзареулян

Your code reminded me of my old library, mustbe. With mustbe, the code looks like this:

func SomeBigFunctionWithCollect() (i int, err error) {
  defer mustbe.Catched(func(e error) {
    fmt.Println("Error in SomeBigFunction:", e)
    err = e
  })

  mustbe.OK(SomeErrorProneFunction())
  mustbe.OKVal(AnotherFunc())
  // ...

  i = mustbe.OKVal(LastFunc()).(int)

  return
}

It's a bit more verbose, but it works already in Go1. Under the hood it uses panics, of course.