我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

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?

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
}

近期文章

  • 如何将unix时间戳格式化为RFC3339-Golang?
  • 使用基本auth和formvalue进行Http Post请求
  • Go中的预定轮询任务
  • 如何在HTTP GET请求中添加URL查询参数?
  • 遍历通道发送的所有值,直到在Go中将其关闭为止
  • 在Golang中解码JWT令牌
  • 将参数传递给http.HandlerFunc
  • 如何在Delve中通过源文件行号设置断点?
  • 如何删除cookie
  • 使用Go确定Stdin是否具有数据
  • Go客户端程序在TIME_WAIT状态下生成大量套接字
  • Go,Golang:遍历struct
  • 复制引用到指针或按值
  • 无法引用未导出的名称m.β
  • SQLite3错误:数据库被锁定在Golang中
  • 如何在GoLand中运行整个项目?
  • 去-打印项目之间没有空格
  • 如何正确使用。调用反射包
  • Go编译器(gc)是否可以进行垃圾回收?
  • 并行循环

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华