patterngoCritical
Golang converting string to int64
Viewed 0 times
int64stringgolangconverting
Problem
I want to convert a string to an int64. What I find from the
The ParseInt actually returns an int64:
So if I want to get an int64 from a string, should I avoid using Atoi, instead use ParseInt? Or is there an Atio64 hidden somewhere?
strconv package is the Atoi function. It seems to cast a string to an int and return it:// Atoi is shorthand for ParseInt(s, 10, 0).
func Atoi(s string) (i int, err error) {
i64, err := ParseInt(s, 10, 0)
return int(i64), err
}The ParseInt actually returns an int64:
func ParseInt(s string, base int, bitSize int) (i int64, err error){
//...
}So if I want to get an int64 from a string, should I avoid using Atoi, instead use ParseInt? Or is there an Atio64 hidden somewhere?
Solution
No, there's no Atoi64. You should also pass in the 64 as the last parameter to ParseInt, or it might not produce the expected value on a 32-bit system.
https://play.golang.org/p/FUC8QO0-lYn
var s string = "9223372036854775807"
i, _ := strconv.ParseInt(s, 10, 64)
fmt.Printf("val: %v ; type: %[1]T\n", i)https://play.golang.org/p/FUC8QO0-lYn
Code Snippets
var s string = "9223372036854775807"
i, _ := strconv.ParseInt(s, 10, 64)
fmt.Printf("val: %v ; type: %[1]T\n", i)Context
Stack Overflow Q#21532113, score: 124
Revisions (0)
No revisions yet.