dongru3726 2017-04-13 08:57
浏览 36
已采纳

Golang模棱两可的错误重新定义

Why is it possible to redefine the err variable?

err := ipdf.Open(source)
if err != nil {
    panic("Couldn't open pdf.")
}

payload, err := ioutil.ReadFile(other)
if err != nil {
    panic("Couldn't read other file.")
}
  • 写回答

3条回答 默认 最新

  • duanmorong9597 2017-04-13 08:59
    关注

    Unlike regular variable declarations, a short variable declaration may redeclare variables provided they were originally declared earlier in the same block (or the parameter lists if the block is the function body) with the same type, and at least one of the non-blank variables is new. As a consequence, redeclaration can only appear in a multi-variable short declaration. Redeclaration does not introduce a new variable; it just assigns a new value to the original.

    https://golang.org/ref/spec#Short_variable_declarations

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?