在Go中未定义的变量

huangapple go评论84阅读模式
英文:

Undefined variable in Go

问题

我遇到了一个错误:在编译过程中出现了"undefined: req"的错误。我知道为什么会出现这个错误,但我不确定如何解决它。这是我的代码:

switch path {
case "user.save":
    var req SaveRequest
case "user.update": 
    var req UpdateRequest
}

err := c.BindJSON(&req)
if err != nil {
    c.JSON(http.StatusOK, gin.H{"error_code": "SERVER_ERROR", "message": "Request is not valid JSON"})
    return   
}

c.Set("req", req)

我试图解析JSON请求,然后将它们添加到上下文中。我意识到,如果我在switch语句之前定义req变量,那么这个问题就可以解决,但我不知道最初应该将它声明为什么类型?

英文:

I'm getting an error: undefined: req during compilation. I understand why I'm getting the error but I'm not sure how to overcome it. This is my code:

switch path {
case "user.save":
    var req SaveRequest
case "user.update": 
    var req UpdateRequest
}
          
err := c.BindJSON(&req)
if  err != nil {
    c.JSON(http.StatusOK, gin.H{"error_code": "SERVER_ERROR", "message": "Request is not valid JSON"})
    return   
}

c.Set("req", req)

I'm trying to parse JSON requests and then add them to the context. I reaslise that if I define my req variable before the switch statement then this should overcome the problem but I don't know what type to declare it as initially?

答案1

得分: 4

Go使用块进行词法作用域(lexical scoped using blocks)。这意味着req的声明仅在其代码块内可见,在这种情况下是case块。

您需要在与您想要使用它的相同作用域或外部作用域中声明req

var req interface{}
switch path {
case "user.save":
    req = &SaveRequest{}
case "user.update":
    req = &UpdateRequest{}
}

err := c.BindJSON(req)
英文:

Go is lexically scoped using blocks. This means that the declarations of req are only visible within their code block, the case block in this instance.

You need to declare req in the same scope, or an outer scope, from where you want to use it.

var req interface{}
switch path {
case "user.save":
    req = &SaveRequest{}
case "user.update": 
    req = &UpdateRequest{}
}

err := c.BindJSON(req)

答案2

得分: 2

这是因为你在switch语句中声明了它。不能保证在err := c.BindJSON(&req)这一行执行时,这两个变量都已经定义。也许你根据输入的某些外部理解知道这不会发生,但编译器无法知道。因此,你在每个case语句中声明的变量的作用域仅限于该语句。

你只需要进行一些重构。你可以将err := c.BindJSON(&req)移到case语句中。你可以使用接口以更通用的方式声明变量,并在后面处理其类型等。按照你认为最好理解的方式进行操作。可能你在其他具有类似类型系统的语言中也遇到过类似情况(例如C、C++、C#和Java)。

英文:

It's because you declare it in a switch statement. There is no guarantee that either will have been defined by the time the err := c.BindJSON(&req) line executes. You may know, based on some outside understanding of the input, that won't happen but the compiler cannot. The variables you declare within each case statement are therefor scoped for that statement.

You just need to do a little refactoring. You could move this err := c.BindJSON(&req) into the case statements. You could use an interface to declare the variable more generically and deal with it's type later on ect. Do what you think reads best. Likely you've encountered this in other languages with somewhat similar type systems (same happens in C, C++, C# and Java for example).

答案3

得分: 2

将你的BindJSON逻辑移到switch语句中:

var err error
switch path {
case "user.save":
    var req SaveRequest
    err = c.BindJSON(&req)
case "user.update": 
    var req UpdateRequest
    err = c.BindJSON(&req)
}

if err != nil {
    c.JSON(http.StatusOK, gin.H{"error_code": "SERVER_ERROR", "message": "请求不是有效的 JSON"})
    return   
}

请注意,我已将错误消息从英文翻译为中文。

英文:

Move your BindJSON logic into the switch statement:

var err error
switch path {
case "user.save":
    var req SaveRequest
    err = c.BindJSON(&req)
case "user.update": 
    var req UpdateRequest
    err = c.BindJSON(&req)
}

if  err != nil {
    c.JSON(http.StatusOK, gin.H{"error_code": "SERVER_ERROR", "message": "Request is not valid JSON"})
    return   
}

huangapple
  • 本文由 发表于 2015年12月10日 02:11:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/34186008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定