dpl74687 2018-04-09 10:42
浏览 5

错误值在if语句外消失

I have the code below:

if err == nil {
    body, err := ioutil.ReadAll(response.Body)
    if err == nil {
        dataMap := &models.UserResponse{}
        json.Unmarshal(body, &dataMap)
        if dataMap.User == (models.UserId{}) {
            err = fmt.Errorf("unauthorized")
            fmt.Println(err) // when unathorized, prints unauthorized
        }
    }
}

fmt.Println(err) // always prints nil

The Println inside if dataMap.User ... prints "unauthorized", whereas the last Println always prints nil.

I have no idea why it happens, err is declared via var err error at the beginning of this function.

  • 写回答

2条回答 默认 最新

  • duanpo2037 2018-04-09 10:46
    关注

    The cause is detailed in Spec: Short variable declaration:

    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.

    When using short variable declaration with multiple variables where one already exists, assignment will only happen to the existing variable if it was declared in the same block. Since in your case err variable existed before the if block, a new err variable will be created inside the if block, which has nothing to do with the "outsider" err variable (other than sharing its name). The outer err will be shadowed in the if block after the short variable declaration.

    So what happens is that inside the if, you create a new err variable, and you assign a value to that, and you print that.

    After the if statement, you will print the outer err variable whose value was not changed inside the if block, so it remains nil.

    See this example:

    var err error
    fmt.Println("outside:", err) // nil
    
    {
        // A new err variable, shadows the outer:
        i, err := strconv.Atoi("a")
        fmt.Println("inside:", i, err) // 0 strconv.Aoti: parsing "a": invalid syntax
    }
    
    fmt.Println("outside:", err) // nil, because this was never changed
    
    // Now this will change the "outer" err:
    j, err := strconv.Atoi("a")
    fmt.Println("outside:", j, err) // 0 strconv.Aoti: parsing "a": invalid syntax
    

    Output (try it on the Go Playground):

    outside: <nil>
    inside: 0 strconv.Atoi: parsing "a": invalid syntax
    outside: <nil>
    outside: 0 strconv.Atoi: parsing "a": invalid syntax
    

    If you want to use (assign to) the "outer" variable when also creating new variable(s), you can't use short variable declaration in a "nested" block but only simple assignment, in which case you also have to declare the other variables a priori, like in this example:

    if err == nil {
        var body []byte
        body, err = ioutil.ReadAll(response.Body)
        // ... rest...
    }
    

    See related questions:

    Why it is possible to redefine err in multiple return statement in Go

    Why there are two ways of declaring variables in Go, what's the difference and which to use?

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)