1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

defer内でのエラーを考慮する。

Posted at

はじめに

  • defer句でエラーが起きたらどうするか

結論

  • ※個人的に良いなと思ったものです。
  • close時に発生したエラーとオリジナルのエラーの両方をログなどで確認することができる。
defer func() {
    deferError := c.Close() //c.Closeはエラーを返す可能性があります。
    if deferError == nil {
      return
    }
    err = fmt.Errorf("close: %v,  original: %v", deferError, err)
}()

//よくない例
defer c.Close()

参考

Go 言語(Golang) はまりどころと解決策

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?