dqu92800 2015-09-21 18:37
浏览 15
已采纳

为什么在上班时不检查零?

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条回答 默认 最新

  • dongli8979 2015-09-21 18:38
    关注

    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;
    {
        // ...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答