为什么在Go语言中这个检查是否为nil的代码不起作用?

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

Why doesn't this check for nil in go work?

问题

在第一个代码示例中,我在“if pr != nil”这一行遇到了错误:

for sup, _ := range supervisorToColor {
    pr := emailToPerson[sup]
    // 下面这行代码导致了编译错误:
    // ./myprog.go:1046: missing condition in if statement
    // ./myprog.go:1046: pr != nil evaluated but not used
    if pr != nil 
    {   
        local := peopleOnFloor[pr.Email]
        sp := &Super{pr, local}
        names = append(names, sp) 
    }   
}   

如果我注释掉nil检查的if语句,它就可以正常编译:

for sup, _ := range supervisorToColor {
    pr := emailToPerson[sup]
    // if pr != nil 
    // {
        local := peopleOnFloor[pr.Email]
        sp := &Super{pr, local}
        names = append(names, sp) 
    // }
}   

起初我倾向于认为这是代码中早期的某个语法错误,但是当我注释掉这些行后它可以正常工作,这让我觉得问题可能出在其他地方。

emailToPerson的类型是map[string]*Person,其中Person是一个结构体。

提前感谢您的帮助。如果这个问题最终变得非常简单,请原谅。

英文:

In the first code example, I get errors for the "if pr != nil" line:

for sup, _ := range supervisorToColor {
        pr := emailToPerson[sup]
        // The line below causes the compilation error:
        // ./myprog.go:1046: missing condition in if statement
        // ./myprog.go:1046: pr != nil evaluated but not used
        if pr != nil 
        {   
          local := peopleOnFloor[pr.Email]
          sp := &Super{pr, local}
          names = append(names, sp) 
        }   
}   

If I comment out the nil check if statement, it compiles fine:

for sup, _ := range supervisorToColor {
        pr := emailToPerson[sup]
        // if pr != nil 
        // {
          local := peopleOnFloor[pr.Email]
          sp := &Super{pr, local}
          names = append(names, sp) 
        // }
}   

At first I was inclined to think it was some syntax error earlier in the code, but the fact that it works when I comment out the lines makes me think it's something else.

emailToPerson is of type map[string]*Person where Person is a struct

Thanks in advance. Apologies if this turns out to be something incredibly simple.

答案1

得分: 6

开放的大括号需要与if在同一行上:

if pr != nil { 

根据Go规范中的分号

> 正式的语法在许多产生式中使用分号“;”作为终止符号。Go程序可以使用以下两个规则省略大部分分号:

> 1. 当输入被分解为标记时,如果该行的最后一个标记是

> - 标识符

  • 整数、浮点数、虚数、符文或字符串字面量
  • 关键字breakcontinuefallthroughreturn
  • 运算符和分隔符++--)]}

则会自动在标记流中的该标记之后插入一个分号。

> 2. 为了允许复杂语句占据一行,可以在闭合的“)”或“}`”之前省略分号。

这意味着你的代码等价于:

if pr != nil;
{
    // ...
}
英文:

The open curly brace needs to be on the same line as the if:

if pr != nil { 

From the Go spec on semicolons:

> The formal grammar uses semicolons ";" as terminators in a number of productions. Go programs may omit most of these semicolons using the following two rules:

> 1. When the input is broken into tokens, a semicolon is automatically inserted into the token stream immediately after a line's final token if that token is

> - an identifier

  • an integer, floating-point, imaginary, rune, or string literal
  • one of the keywords break, continue, fallthrough, or return
  • one of the operators and delimiters ++, --, ), ], or }

> 2. To allow complex statements to occupy a single line, a semicolon may be omitted before a closing ")" or "}".

This means that your code was equivalent to:

if pr != nil;
{
    // ...
}

huangapple
  • 本文由 发表于 2015年9月22日 02:37:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/32702338.html
匿名

发表评论

匿名网友

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

确定