我有cmd prompt,其中stdin只接受字符串,但是一旦我收到字符串就需要convert to float。当有人错误输入时,"0..1" instead of 0.1,我需要检查并显示错误信息。
msg := "enter the rate eg:{0.1}"
rate, err := RatePrompt(msg)
if err != nil {
fmt.Println("something went while entering rate, please re-enter")
}
func RatePrompt(cmrmsg string) (price string, err error) {
fmt.Println(" ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
price := scanner.Text()
return price, nil
}
//check for string contains multiple dot
rate, err := RatePrompt(msg)
if err != nil {
fmt.Println("something went while entering
rate, please re-enter")else{
///check for string contains multiple dot
}
}
检查字符串值中是否有多个点,如果存在多个点,则抛出错误
斯蒂芬大帝
相关分类