Try & Catch sounds great! 在2022年5月19日星期四 UTC+8 00:15:09<har...@gmail.com> 写道:
> Hi all, > > I thought now was the time to go public. The automatic error propagation > is possible with the help of a simple Go package, and it has been about > three years now: > > func CopyFile(src, dst string) (err error) { > defer err2.Returnf(&err, "copy %s %s", src, dst) > > r := try.To1(os.Open(src)) > defer r.Close() > > w := try.To1(os.Create(dst)) > defer err2.Handle(&err, func() { > _ = os.Remove(dst) > }) > defer w.Close() > > try.To1(io.Copy(w, r)) > return nil > } > > The playground <https://go.dev/play/p/bUGX8yDAeoL>. > > A couple of blog posts about the subject: > > - Error Propagation > > <https://hackernoon.com/golang-more-error-handling-with-less-checking-eeh31go> > - Errors as Discriminated Unions > <https://hackernoon.com/billion-dollar-mistake-in-go-ll1s3tkc> > > I'm the author of those posts and the err2 package > <https://github.com/lainio/err2>. Additional sources for original ideas > are mentioned in the blogs and the package documentation. And, of course, > pure error values are needed when transporting errors through channels. > > I hope that any gopher who tries the err2 package finds it as valuable and > productive > as we have. Indeed, Go is one of the most pragmatic and refactor-friendly > native languages. > > Best regards, > -Harri > -- 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/27a9b63c-ad67-475d-851a-97fadb94b3dfn%40googlegroups.com.