未定义的err变量

编程入门 行业动态 更新时间:2024-10-25 08:22:43
本文介绍了未定义的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)

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

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) }

或使用一行来声明user和err:

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变量

本文发布于:2023-11-11 10:48:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1578209.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   未定义   err

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!