Short variable declaration and “variable declared and not used” error

前端 未结 2 1191
离开以前
离开以前 2021-01-23 08:18

I\'ve stumbled across a strange issue where the code below fails to compile:

func main() {
    var val reflect.Value
    var tm time.Time

    if tm, err := time         


        
2条回答
  •  不要未来只要你来
    2021-01-23 09:14

    This part:

    if tm, err := time.Parse(...)
    

    creates a new variable tm that has scope only within the if statement - it is NOT the one you declared as var tm time.Time.

    This new variable is not used within the if, therefore you get the error. Note you also don't get the outer-level tm assigned, so fmt.Println will print the zero time, not what time.Parse returned.

    To fix this: declare err and change your if to read:

    var err error
    if tm, err = time.Parse(...)
    

    NOTE this is a subtle thing in GO and a fairly common source of mistakes. The := statement can in fact be used with a mix of variables that are already declared and one or more new variables - if the already-declared ones are in the same lexical scope. Then, only the new ones are auto-declared by := and the rest are just assigned (as with =). However, if you use := in a new scope, then ALL variables are declared in that scope and mask any outer-scope variables with the same name (such as in an if; note that the if condition is not inside the braces, but is still considered as if it were within the {code} block; same happens with the for and other compound statements in GO).

提交回复
热议问题