Check if String is Numeric in Go
In Go language, you can use functions in the strconv package to determine if a string is a number. The specific steps are as follows:
package main
import (
"fmt"
"strconv"
)
func main() {
str := "12345"
_, err := strconv.Atoi(str)
if err == nil {
fmt.Println("字符串是数字")
} else {
fmt.Println("字符串不是数字")
}
}
In the above example, we used the strconv.Atoi() function to convert a string into an integer and check for any errors. If there are no errors, it means the string is a number; otherwise, it is not.