I'm trying to do comparisons on big numbers but can only get a the string value. So how do you do a condition on a big.Int
. Below is the closest to what I have tried.
package main
import (
"fmt"
"math/big"
)
func main() {
dirtyVal := "9446744073709551615"
dv := big.NewInt(0)
dv.SetString(dirtyVal, 10)
userVal := dv.String()
maxVal := "18446744073709551615"
mv := big.NewInt(0)
mv.SetString(maxVal, 10)
// maxValue := mv.String()
if userVal > maxVal {
fmt.Println("True")
} else {
fmt.Println("False")
}
}
You can use func (*Int) Cmp
to compare two big.Int http://golang.org/pkg/math/big/#Int.Cmp
if dv.Cmp(mv)>0 {
fmt.Println("True")
} else {
fmt.Println("False")
}