Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang ambiguous err redefinition

Tags:

definition

go

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.")
}
like image 452
yshadow Avatar asked Mar 09 '23 08:03

yshadow


1 Answers

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

like image 175
zerkms Avatar answered Mar 20 '23 20:03

zerkms