Golang 歧义 err 重定义

标签 go definition

为什么可以重新定义 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.

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

关于Golang 歧义 err 重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43387743/

相关文章:

C++ 命名空间和多重定义的变量

c++ - 定义动态结构

go - 使用 Golang 的 Google Cloud Functions 工作流

loops - Go:比较两个 slice 并删除多个索引

go - golang执行命令出错

PHP脚本语言还是编程语言?

c++ - C++ OOP初学者,分配问题

c - 预处理器宏定义是否需要在头文件中大写?

golang gob 将指向0的指针转换为nil指针

postgresql - Sqlx 获取准备好的语句