func Confirm(prompt, cancel string) (bool, error) {标签:return,err,nil,fmt,扫描,var,正则,go,input From: https://www.cnblogs.com/cheyunhua/p/16938440.html
var yesRx = regexp.MustCompile("^(?:y(?:es)?)$")
var noRx = regexp.MustCompile("^(?:n(?:o)?)$")
var input string
for {
fmt.Printf("%s Yes [y/yes], No [n/no] : ", prompt)
_, err := fmt.Scanln(&input)
if err != nil {
return false, err
}
if yesRx.MatchString(input) {
return true, nil
}
if noRx.MatchString(input) {
fmt.Print(cancel)
return false, nil
}
}
}