如何在围棋中判断字符串值是否为整数?
像这样的东西
v := "4"
if isInt(v) {
fmt.Println("We have an int, we can safely cast this with strconv")
}
注意:我知道strconv.Atoi
会返回错误,但是有没有其他函数可以做到这一点呢?
strconv.Atoi
的问题是它将返回"a7"
的7
如何在围棋中判断字符串值是否为整数?
像这样的东西
v := "4"
if isInt(v) {
fmt.Println("We have an int, we can safely cast this with strconv")
}
注意:我知道strconv.Atoi
会返回错误,但是有没有其他函数可以做到这一点呢?
strconv.Atoi
的问题是它将返回"a7"
的7
就像你说的,你可以用strcom.Atoi来做这件事.
if _, err := strconv.Atoi(v); err == nil {
fmt.Printf("%q looks like a number.\n", v)
}
您可以在模式ScanInts
中使用scanner.Scanner
(从text/scanner
开始),也可以使用regexp来验证字符串,但是Atoi
是适合这项工作的工具.