https://golangdocs.com/converting-string-to-integer-in-golang
In this post, we will cover string to integer conversion in GoLang.
strconv package
Package strconv implements conversions to and from string representations of basic data types.
Atoi或者Itoa这里的a可以理解为ascii
同时我也有一个问题,如果返回值中需要带着error, error都作为最后一个返回值吗?
ParseInt
函数签名如下:
func ParseInt(s string, base int, bitSize int) (i int64, err error)
bitSize参数是什么?可以使用的值有0,8,16,32,64
parseInt会不会出现解析精度丢失的问题?
// The bitSize argument specifies the integer type
// that the result must fit into. Bit sizes 0, 8, 16, 32, and 64
// correspond to int, int8, int16, int32, and int64.
// If bitSize is below 0 or above 64, an error is returned.
//
string 转 int
// Atoi is equivalent to ParseInt(s, 10, 0), converted to type int.
func Atoi(s string) (int, error)
int 转string
// Itoa is equivalent to FormatInt(int64(i), 10).
func Itoa(i int) string {
return FormatInt(int64(i), 10)
}