Javascript's Boolean translation to Golang

-2

In JavaScript I see functions like:

function SomeFunc(i) {
    var f = 0x80000000;

    return Boolean(i & f);
}

What would be an analog in Golang? First of all, I see 0x80000000 is not possible, syntax is the second question

go
asked on Stack Overflow Dec 10, 2018 by Alexey

2 Answers

2

In JavaScript, coercing any non-boolean value to boolean just does a "falsiness" check; for the most part, any non-empty value is false and everything else is true. So the Go equivalent for an integer value would simply be:

return i != 0
answered on Stack Overflow Dec 10, 2018 by Adrian • edited Dec 10, 2018 by Adrian
1

To put it all together

package main

import (
    "fmt"
)
//function SomeFunc(i) {
//    var f = 0x80000000;
//
 //   return Boolean(i & f);
//}

func SomeFunc(i uint64) bool{
 return i & 0x80000000 != 0
}

func main() {
    fmt.Println(SomeFunc(0x800))
    fmt.Println(SomeFunc(0x81234567))
}
answered on Stack Overflow Dec 10, 2018 by Vorsprung

User contributions licensed under CC BY-SA 3.0