switch_input.go 1004 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  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":
  19. fmt.Println("Welcome Philip!")
  20. case "Chris\r\n":
  21. fmt.Println("Welcome Chris!")
  22. case "Ivo\r\n":
  23. fmt.Println("Welcome Ivo!")
  24. default:
  25. fmt.Printf("You are not welcome here! Goodbye!")
  26. }
  27. // version 2:
  28. switch input {
  29. case "Philip\r\n":
  30. fallthrough
  31. case "Ivo\r\n":
  32. fallthrough
  33. case "Chris\r\n":
  34. fmt.Printf("Welcome %s\n", input)
  35. default:
  36. fmt.Printf("You are not welcome here! Goodbye!\n")
  37. }
  38. // version 3:
  39. switch input {
  40. case "Philip\r\n", "Ivo\r\n":
  41. fmt.Printf("Welcome %s\n", input)
  42. default:
  43. fmt.Printf("You are not welcome here! Goodbye!\n")
  44. }
  45. }