Search code examples
gostrconv

Idiomatic way to use strconv.ParseInt with ints


I end up writing code like this. I need a regular int (for some other function call), but parseInt only produces 64 bit ints:

i64, err := strconv.ParseInt(s, 10, 0)
if err != nil {
    fmt.Println("parsing failed for", s)
}
i := int(i64)

http://play.golang.org/p/_efRm7yp3o

Is there a way to avoid the extra cast? Or a way to make this more idiomatic?


Solution

  • You can use strconv.Atoi which wraps strconv.ParseInt in a way you want.