Is there a way to convert integers to bools in go or vice versa?

44,278

Solution 1

Int to bool is easy, just x != 0 will do the trick. To go the other way, since Go doesn't support a ternary operator, you'd have to do:

var x int
if b {
    x = 1
} else {
    x = 0
}

You could of course put this in a function:

func Btoi(b bool) int {
    if b {
        return 1
    }
    return 0
 }

There are so many possible boolean interpretations of integers, none of them necessarily natural, that it sort of makes sense to have to say what you mean.

In my experience (YMMV), you don't have to do this often if you're writing good code. It's appealing sometimes to be able to write a mathematical expression based on a boolean, but your maintainers will thank you for avoiding it.

Solution 2

Here's a trick to convert from int to bool:

x := 0
newBool := x != 0 // returns false

where x is the int variable you want to convert from.

Solution 3

var a int = 3
var b bool = a != 0

I just dropped this into the demo box on the golang front page:

package main

import "fmt"

func main() {
 var a int = 3
 var b bool = a != 0
    fmt.Println("Hello, 世界", b)
}

Output:

Hello, 世界 true

Solution 4

There are no conversions from bool to integer types or vice versa.

Use the inequality operator to convert integers to bool values:

b := i != 0

Use an if statement to convert a bool to an integer type:

var i int
if b {
    i = 1
}

Because the zero value for integer types is 0, the else branch shown in other answers is not necessary.

Solution 5

Just to show TMTOWTDT

package main

import (
    "fmt"
    "unsafe"
)

func main() {
    fmt.Println(bool2int(true))
    fmt.Println(bool2int(false))
}

func bool2int(a bool) uint64 {
    return *(*uint64)(unsafe.Pointer(&a))&1
}

https://play.golang.org/p/eULE6cJO_c0

Share:
44,278

Related videos on Youtube

worr
Author by

worr

Updated on July 09, 2022

Comments

  • worr
    worr almost 2 years

    Is there a builtin way to cast bools to integers or vice versa? I've tried normal casting, but since they use different underlying types, conversion isn't possible the classic way. I've poured over some of the specification, and I haven't found an answer yet.

  • Everton
    Everton over 5 years
    Cool. Does this work across platforms? I mean, Go encodes the boolean as the least significant bit across platforms?
  • Vorsprung
    Vorsprung over 5 years
    I think that on most (Intel) platforms we are little endian and the code above will work. To make it more portable I guess that you could add a step to convert to Network order which is always big endian and then look at the other end of the bytes
  • Admin
    Admin almost 4 years
    A boolean, numeric, or string type can be set to another type. For[]byte see stackoverflow.com/a/62725637/12817546, for float64 see stackoverflow.com/a/62753031/12817546, int see stackoverflow.com/a/62737936/12817546, []rune see stackoverflow.com/a/62739051/12817546, and for string see stackoverflow.com/a/62740786/12817546.
  • Pierre Prinetti
    Pierre Prinetti almost 4 years
    newBool := x != 0
  • Bjorn
    Bjorn over 3 years
    Note that the else in the first example is unnecessary as x will be initialized to 0, as demonstrated in the answer by Cerise Limón.
  • jsgoupil
    jsgoupil almost 3 years
    If I see this in a company code, I will git blame it then report it to HR.
  • Vorsprung
    Vorsprung almost 3 years
    @jsgoupil haha yes, this way of doing it is a pretty bad idea!