switch_input.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "bufio"
  6. )
  7. func main() {
  8. inputReader := bufio.NewReader(os.Stdin)
  9. fmt.Println("Please enter your name:")
  10. input, err := inputReader.ReadString('\n')
  11. if err != nil {
  12. fmt.Println("There were errors reading, exiting program.")
  13. return
  14. }
  15. fmt.Printf("Your name is %s", input)
  16. // For Unix: test with delimiter "\n", for Windows: test with "\r\n"
  17. switch input {
  18. case "Philip\r\n": fmt.Println("Welcome Philip!")
  19. case "Chris\r\n": fmt.Println("Welcome Chris!")
  20. case "Ivo\r\n": fmt.Println("Welcome Ivo!")
  21. default: fmt.Printf("You are not welcome here! Goodbye!")
  22. }
  23. // version 2:
  24. switch input {
  25. case "Philip\r\n": fallthrough
  26. case "Ivo\r\n": fallthrough
  27. case "Chris\r\n": fmt.Printf("Welcome %s\n", input)
  28. default: fmt.Printf("You are not welcome here! Goodbye!\n")
  29. }
  30. // version 3:
  31. switch input {
  32. case "Philip\r\n", "Ivo\r\n": fmt.Printf("Welcome %s\n", input)
  33. default: fmt.Printf("You are not welcome here! Goodbye!\n")
  34. }
  35. }