且构网

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

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

更新时间:2023-01-16 18:15:18

相信你要找的功能是

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])
}