Created
March 7, 2019 05:07
-
-
Save udhos/923078025f9908f16bacbf98c2e99e8b to your computer and use it in GitHub Desktop.
bool2int golang unsafe trick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://stackoverflow.com/a/51097467/1011695 | |
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment