简体   繁体   中英

Intercepting errors and panics in Go

I'm creating a CLI just to learn a bit more about Go. Is it possible to intercept all errors and panics, so I can have one unique place to handle them, and also format it and after print to stdout.

Example:

Instead of using:

x, err := doSomething()
if err != nil {
  errorHandler.Handler(err)
}

I could just use initialize the code that can intercept all errors before doing anything:

errorHandler.New()

And then I could just "ignore" all errors:

x, _ := doSomething()

Is that a bad practice with Go?

Errors are just values in go. Just like there's no way to intercept any time a function returns a string, there's no way to intercept errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM