I want to convert string to integer in golang. But I don't know the format of string. For example, "10" -> 10, "65.0" -> 65, "xx" -> 0, "11xx" -> 11, "xx11"->0
I do some searching and find strconv.ParseInt(). But it can not handle "65.0". So I have to check string's format.
Is there a better way?
解决方案
I believe function you are looking for is
strconv.ParseFloat()
see example here
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])
}