douqi1928 2017-08-06 17:46
浏览 167
已采纳

Go中if条件的非布尔值

I have an if statement in Go which looks like this:

if level & 1 {
    // do something
} else {
    // do something else
}

The level variable in my cause is of type uint. But when I do bitwise AND with 1, the result is not a boolean. This is a valid syntax for C, but apparently it doesn't work in Go. Any idea how to work around this?

  • 写回答

1条回答 默认 最新

  • doubaoguo7469 2017-08-06 17:47
    关注

    If statements in Go must have type of bool, which you can achieve by using a comparison operator, the result of which is a bool:

    if level&1 != 0 {
        // do something
    } else {
        // do something else
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置