在Go中更简单的错误处理 [英] More terse error handling in Go

查看:200
本文介绍了在Go中更简单的错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我看看我的代码,发现它有很多错误处理程序:

  err = result.Scan(& bot.BID& bot.LANGUAGE,& bot.SOURCE)
如果err!= nil {
log.Fatalf(result.Scan:%v,err)
return
}

fileName,err:= copySourceToTemporaryFile(bot)
如果err!= nil {
log.Fatalf(copySourceToTemporaryFile:%v,err)
return
}
...

很多行看起来像:

  // do something 
// handle error
// handle error
// handle error

// do something 2
//句柄错误
//句柄错误
//句柄错误

我可以创建打印出错误并停止处理的默认处理程序,或至少将我的代码逻辑中的错误处理程序垃圾移出来。

解决方案

这让我想起了最近的< Rob Pike的一个href =https://blog.golang.org/errors-are-values =noreferrer>错误是值,以及先生。 Rob Pike教导我关于Go Go 2014年的Go的错误处理实践


然而,关键的教训是错误是值和Go的编程语言的全部功能可用于处理它们。值得强调的是,无论设计如何,程序检查错误是非常重要的。这里的讨论不是关于如何避免检查错误,而是关于使用该语言来处理错误的恩典。


一种技术是定义一个名为 errWriter 的对象:

 键入errWriter struct {
w io.Writer
err错误
}




写入方法调用Write方法 Writer 并记录第一个错误以备将来参考:




  func(ew * errWriter)write(buf [] byte){
如果ew.err!= nil {
return
}
_,ew .err = ew.w.Write(buf)
}




一旦发生错误,写入方法变为无操作,但保存错误值。



给定 errWriter 类型及其写入方法,上述代码可以重构:




  ew:=& errWriter {w:fd} 
ew.write(p0 [a:b ])
ew.write(p1 [c:d] )
ew.write(p2 [e:f])
//等等
如果ew.err!= nil {
return ew.err
}


How do I handle a lot of errors in Go?

I look at my code and find that it is full of error handlers:

err = result.Scan(&bot.BID, &bot.LANGUAGE, &bot.SOURCE)
if err != nil {
    log.Fatalf("result.Scan: %v", err)
    return
}

fileName, err := copySourceToTemporaryFile(bot)
if err != nil {
    log.Fatalf("copySourceToTemporaryFile: %v", err)
    return
}
...

And a lot of lines look like:

// do something
// handle error
// handle error
// handle error

// do something 2
// handle error
// handle error
// handle error

Can I create a default handler that prints an error and stops processing, or at least move out this "error-handler-garbage" out of my code logic?

解决方案

That reminds me of the recent Errors are values by Rob Pike, as well as Mr. Rob Pike taught me about practice of error handling in Go at GoCon 2014

The key lesson, however, is that errors are values and the full power of the Go programming language is available for processing them.

It's worth stressing that whatever the design, it's critical that the program check the errors however they are exposed. The discussion here is not about how to avoid checking errors, it's about using the language to handle errors with grace.

One technique was to define an object called an errWriter:

type errWriter struct {
    w   io.Writer
    err error
}

The write method calls the Write method of the underlying Writer and records the first error for future reference:

func (ew *errWriter) write(buf []byte) {
    if ew.err != nil {
        return
    }
    _, ew.err = ew.w.Write(buf)
}

As soon as an error occurs, the write method becomes a no-op but the error value is saved.

Given the errWriter type and its write method, the code above can be refactored:

ew := &errWriter{w: fd}
ew.write(p0[a:b])
ew.write(p1[c:d])
ew.write(p2[e:f])
// and so on
if ew.err != nil {
    return ew.err
}

这篇关于在Go中更简单的错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆