// Chapter 21 example: a validation loop where 0 is a good answer, and which // stops when the input runs out. // Run with: go run exercises/21/09-validate-volume.go package main import ( "bufio" "fmt" "os" "strconv" "strings" ) func main() { reader := bufio.NewReader(os.Stdin) var volume int for { fmt.Print("Volume (0 to 10): ") text, err := reader.ReadString('\n') line := strings.TrimSpace(text) if err != nil && line == "" { fmt.Println("No more input.") return } volume, err = strconv.Atoi(line) if err == nil && volume >= 0 && volume <= 10 { break } fmt.Println("That is not a volume from 0 to 10.") } fmt.Println("Volume set to", volume) }