未定义的err变量 [英] Undefined err variable

查看:77
本文介绍了未定义的err变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Go的新手",我不确定为什么在编译程序时会在控制台中收到错误 undefined err unfinded user . >

我有:

if req.Id == nil {
    user, err := signup(C, c, &req)
} else {
    user, err := update(C, c, &req)
}

if err != nil {
    c.JSON(http.StatusOK, err)
    return
}

doSomethingWith(user)

我意识到我可以在条件块之前声明erruser变量,但是我想知道为什么这不起作用.一次性创建两个新变量是否有关系?

UDPATE 搞砸了这个.

我现在有:

user := core.User{}
if req.Id == nil {
    user, err := signup(C, c, &req)
} else {
    user, err := update(C, c, &req)
}

cleanUser(&user)

并且我的错误现在是用户声明且未使用.我目前不解决 err 部分,但不确定为什么我会遇到有关用户的错误.

解决方案

这是因为您正在创建的err变量的范围:它仅在范围内(因此有效/可引用),直到最里面的块的结尾是在其中声明的.

规范:声明和范围

在函数内部声明的常量或变量标识符的范围始于ConstSpec或VarSpec的末尾(对于简短变量声明为ShortVarDecl),并结束于最里面的包含块的末尾. /p>

if语句之前声明它时,它将一直作用到容器块的末尾,该容器块还包括第二个if,您可以在其中测试err变量,因此可以. /p>

UDPATE:

更新到您的更新:您使用了简短变量声明,它创建了新变量,因为您使用了在一个新的块中.您尚未使用这些新变量(仅在内部块外部声明了其他" user),因此发生了编译时错误"用户已声明但未使用" .

解决方案很简单:只需在if之前声明两个变量,而不使用短变量声明,而只需分配:

user := core.User{}
var err error

if req.Id == nil {
    user, err = signup(C, c, &req)
} else {
    user, err = update(C, c, &req)
}

if err == nil {
    cleanUser(&user)
}

或使用一行来声明usererr:

user, err := core.User{}, error(nil)

As a Go "newb" I'm not sure why I'm receiving the errors undefined err and undefinded user in the console when compiling the program.

I have:

if req.Id == nil {
    user, err := signup(C, c, &req)
} else {
    user, err := update(C, c, &req)
}

if err != nil {
    c.JSON(http.StatusOK, err)
    return
}

doSomethingWith(user)

I realise I could probably declare the err and user variables before the conditional block but I would like to know why this doesn't work. Is it something to do with creating two new variables in one go?

UDPATE Getting in a bit of a mess with this.

I've now got:

user := core.User{}
if req.Id == nil {
    user, err := signup(C, c, &req)
} else {
    user, err := update(C, c, &req)
}

cleanUser(&user)

and my errors now are user declared and not used. I'm not tackling the err part at the moment but I'm unsure why I'm getting errors about the user.

解决方案

It's because the scope of the err variable you're creating: it is only in scope (and therefore valid/referable) till the end of innermost block in which you declared it.

Spec: Declarations and scope

The scope of a constant or variable identifier declared inside a function begins at the end of the ConstSpec or VarSpec (ShortVarDecl for short variable declarations) and ends at the end of the innermost containing block.

When you declare it before the if statement, then it will be in scope till the end of the container block which also includes the 2nd if where you test the err variable, so that is ok.

UDPATE:

Update to your update: you used a Short variable declaration which creates new variables because you used it in a new block. You haven't used these new variables (only the "other" user declared outside the inner block) hence the compile time error "user declared and not used".

Solution is simple: simply declare both variables before the if and don't use short variable declaration but simply assignment:

user := core.User{}
var err error

if req.Id == nil {
    user, err = signup(C, c, &req)
} else {
    user, err = update(C, c, &req)
}

if err == nil {
    cleanUser(&user)
}

Or using one line to declare both user and err:

user, err := core.User{}, error(nil)

这篇关于未定义的err变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆