且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Golang中将字符串转换为整数

更新时间:2023-02-03 08:46:38

我相信您要查找的函数是

I believe function you are looking for is

strconv.ParseFloat()

请参阅示例此处

但是此函数的返回类型为float64。

But return type of this function is float64.

如果不需要小数作为字符串后面的函数传递的数字的一部分将完成这项工作:

If you don't need fractional part of the number passed as the string following function would do the job:

func StrToInt(str string) (int, error) {
    nonFractionalPart := strings.Split(str, ".")
    return strconv.Atoi(nonFractionalPart[0])
}