错误是值(博客)-逻辑上是相同的吗?

huangapple go评论80阅读模式
英文:

Errors are values (blog) - is logically same?

问题

我刚刚阅读了Rob Pike写的博客。关于这个问题,我有一个小问题,可能我也可能是错的,但我仍然希望得到反馈并正确理解Go语言。

在博客中有一段代码片段(实际上是由@jxck_编写的):

_, err = fd.Write(p0[a:b])
if err != nil {
    return err
}
_, err = fd.Write(p1[c:d])
if err != nil {
    return err
}
_, err = fd.Write(p2[e:f])
if err != nil {
    return err
}
// 以此类推

a) 根据我的理解,上面的代码会在fd.Write(p0[a:b])出现错误时返回,并且不会执行fd.Write(p1[c:d]),对吗?

Rob建议这样写:

var err error
write := func(buf []byte) {
    if err != nil {
        return
    }
    _, err = w.Write(buf)
}
write(p0[a:b])
write(p1[c:d])
write(p2[e:f])
// 以此类推
if err != nil {
    return err
}

b) 基于上面的代码,看起来错误将从子函数返回。这意味着如果在write(p0[a:b])处发生错误,它仍然会执行write(p1[c:d]),对吗?这意味着从逻辑上讲两者不同,对吗?

有人可以解释一下吗?

英文:

I just read the blog written by Rob Pike. I have a small question regarding this and may be I can be wrong too but would still like to get feedback and understand properly the Go.

In the blog there was a snippet code (which actually was written by @jxck_)

_, err = fd.Write(p0[a:b])
if err != nil {
    return err
}
_, err = fd.Write(p1[c:d])
if err != nil {
    return err
}
_, err = fd.Write(p2[e:f])
if err != nil {
    return err
}
// and so on

a) As per my understanding the above code will return if error occurred at fd.Write(p0[a:b]), and will never execute fd.Write(p1[c:d]) , right?

And Rob suggested to write something like this

var err error
write := func(buf []byte) {
    if err != nil {
        return
    }
    _, err = w.Write(buf)
}
write(p0[a:b])
write(p1[c:d])
write(p2[e:f])
// and so on
if err != nil {
    return err
}

b) Based on the above, looks like the error will return from the sub function. So this means if the error occurs at write(p0[a:b]) then still it will execute write(p1[c:d]), right? So this means logically both are not same, right?

Anybody explain.

答案1

得分: 3

不,它们是相同的。如果在fd.Write(p0[a:b])处发生了一个errorerr变量将保存它的值。

现在,如果你调用write(p1[c:d]),那么write()函数将首先检查err != nil,但由于它已经存储了上一次调用中发生的error,它将立即返回并且不会执行进一步的代码。

英文:

No, they are the same. If an error occurs at fd.Write(p0[a:b]), the err variable will hold its value.

Now if you call write(p1[c:d]), then the write() func will first check if err != nil but since it already stores the error which occured in the previous call, it will return immediately and will not execute further code.

答案2

得分: 2

a) 是的,你是正确的。如果错误发生在第一次写入时,它将返回。

b) 不是的。在这个例子中,write 是一个闭包。其中的 err 与外部作用域中的 err 是相同的。所以如果第一次写入失败,其他的写入将会简单地返回,因为外部的 err 不再是 nil

英文:

a) Yes, you are correct. If the error occures in the first write, it will return.

b) No. The write in this example is a closure. The err inside of it is the same as in the outer scope. So if the first write fails, the other will simply return, because the outer err is not nil anymore.

huangapple
  • 本文由 发表于 2015年2月9日 22:56:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/28412630.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定