Go cheatsheet - String to Number

·

1 min read

String to Number

Atoi

k, _ := strconv.Atoi("135")
// k = 135

n, e := strconv.Atoi("wat")
// n = 0, e is not nil

ParseInt

i, _ := strconv.ParseInt("123", 10, 64)
// i = 123
d1, _ := strconv.ParseInt("0x1c8", 0, 64)
// d1 = 456
d2, _ := strconv.ParseInt("1c8", 16, 64)
// d2 = 456
i, err := strconv.ParseInt("10string", 2, 32)
// i = 0, err is not nil

ParseUInt

u, _ := strconv.ParseUint("789", 10, 64)

ParseFloat

f, _ := strconv.ParseFloat("1.234", 64)