variables - 错误值在 if 语句之外消失

标签 variables go scope

我有以下代码:

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

if dataMap.User ... 中的 Println 打印 "unauthorized",而最后一个 Println总是打印nil

我不知道为什么会这样,err 是通过函数开头的 var err error 声明的。

最佳答案

原因详见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.

当对多个已经存在的变量使用短变量声明时,只有在在同一 block 中声明的现有变量才会发生赋值。因为在你的情况下 err if 之前存在变量 block ,一个新的 err变量 将在 if 中创建 block ,与“局外人”无关err变量(除了分享它的名字)。外层err将在 if 中被隐藏在简短的变量声明之后 block 。

那么在 if 内部会发生什么? , 你创建了一个新的 err变量,然后为其分配一个值,然后打印该值。

if之后声明,您将打印外部 err if 中值未更改的变量 block ,所以它仍然是 nil .

看这个例子:

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

输出(在 Go Playground 上尝试):

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

如果你想在创建新变量时使用(分配给)“外部”变量,你不能在“嵌套” block 中使用短变量声明而只能简单赋值,在这种情况下你也必须先验地声明其他变量,如本例所示:

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

查看相关问题:

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?

关于variables - 错误值在 if 语句之外消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49731072/

相关文章:

swift - 错误 - 条件中的变量绑定(bind)需要初始值设定项

php - PDO - 准备好的语句不能正确处理变量

mysql - 软删除级联不起作用

go - 在 Go 中,定义明确的类型的 JSON 编码(marshal)处理会失败吗?

scope - 如何在全局范围内创建 Freemarker 函数?

c++ - 确保具有全局范围的静态变量在任何地方都存在一次

php - 将来自 mysql 查询的单个记录保存到变量

file - 打开和写入文件时出错

javascript - 为什么单击不同的按钮时会出现相同的数组值?

r - 在 R 中编写函数,牢记作用域