小编Ben*_*ade的帖子

为什么不检查nil上班?

在第一个代码示例中,我收到"if pr!= nil"行的错误:

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) 
        }   
}   
Run Code Online (Sandbox Code Playgroud)

如果我注释掉nil check if语句,它编译好:

for sup, _ := range supervisorToColor {
        pr := emailToPerson[sup]
        // if pr != nil 
        // {
          local := peopleOnFloor[pr.Email]
          sp := …
Run Code Online (Sandbox Code Playgroud)

go

0
推荐指数
1
解决办法
88
查看次数

标签 统计

go ×1