Non-bool value in if condition in Go

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

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

The variable levelin my reason is of type uint. But when I beat AND with 1, the result is not logical. This is a valid syntax for C, but apparently it does not work in Go. Any idea how to get around this?

+4
source share
1 answer

If statements in Go must be of typebool , what can you achieve with the comparison operator , the result of which is bool:

if level&1 != 0 {
    // do something
} else {
    // do something else
}
+13
source

Source: https://habr.com/ru/post/1683110/


All Articles