Advertisement
AlexNovoross87

switch-case

May 8th, 2025 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.58 KB | None | 0 0
  1. Базовый пример
  2. x := 3                        url := "example.com"
  3. switch х {                    switch url {
  4. case 1:                       case "example.com":
  5. fmt Printin("1")              fmt.Printin("test")
  6. case 2:                       case "google.com":
  7. fmt.Printin("2")              fmt.Println("live")
  8. case 3:                       default:
  9. fmt Printin("3")              fmt.Printin("dev")
  10. default:                      }
  11. fmt.Println("other:", x)
  12. }
  13.  
  14. Условные варианты
  15. -----------------
  16. switch result := calculate(5); result
  17. {
  18. case result > 10:
  19. fmt. Pr-i ntln (”>10")
  20. case result == 6:
  21. fmt.Println("==6)
  22. case result < 10:
  23. fmt.Println(”<10")
  24. }
  25.  
  26.  
  27. Список вариантов
  28. ----------------
  29. switch х {
  30. case 1, 2, 3:
  31. // ..
  32. case 10, 20, 30:
  33. // ..
  34. }
  35.  
  36. Падение (fallthrough)
  37. ----------------------
  38. fallthrough в Go - это ключевое слово, которое используется внутри блока
  39. case конструкции switch для явного перехода к выполнению следующего
  40. блока case, независимо от его условия.
  41. Когда программа встречает fallthrough, она выполняет следующий case
  42. без проверки его условия
  43.  
  44. switch letter
  45. {
  46.  case ' ': //ничего
  47.  case 'a', 'e', 'i', 'o', 'u':
  48.  fmt.Printin("A vowel")
  49.  fallthrough
  50.  case 'A', 'E', 'I’, 'O', 'U':
  51. fmt.Printin("Vowels are great")
  52. default:
  53. fmt.Println("It's something else")
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement