Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

non-bool value in if condition in Go

Tags:

go

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?

like image 390
typos Avatar asked Aug 06 '17 17:08

typos


1 Answers

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
}
like image 119
Tim Cooper Avatar answered Oct 14 '22 23:10

Tim Cooper