0xBF
0xBF

0xBF

Go cheatsheet - String to Number

0xBF's photo
0xBF

Published on Apr 21, 2021

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)
 
Share this