目录
描述
strconv.Itoa函数用于将int数字转换为字符串。
语法和参数
函数签名
func Itoa(i int) string
参数名称 | 含义 |
i | 待转换为字符串的整型数字 |
返回值:strconv.Itoa函数参数整型数字对应的数字字符串。
使用示例
package main
import (
"fmt"
"strconv"
)
func main() {
result := strconv.Itoa(23)
fmt.Println(result)
// output: 23
}
注意事项
虽然内置函数string也是接收一个整型数字作为参数,并返回一个字符串,但string函数并非将数字转换为对应的数字字符串。更多区别可以参考:《Go string函数与strconv.Itoa函数的区别》https://blog.youkuaiyun.com/TCatTime/article/details/100192117https://blog.youkuaiyun.com/TCatTime/article/details/100192117
源码分析
strconv.Itoa函数将int参数i转换为int64之后,调用了strconv.FormatInt函数。因此strconv.Itoa函数等效于strconv.FormatInt(int64(i), 10)
// Itoa is equivalent to FormatInt(int64(i), 10).
func Itoa(i int) string {
return FormatInt(int64(i), 10)
}