为什么可以重新定义 err
变量?
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.")
}
最佳答案
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.
关于Golang 歧义 err 重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43387743/