Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Базовый пример
- x := 3 url := "example.com"
- switch х { switch url {
- case 1: case "example.com":
- fmt Printin("1") fmt.Printin("test")
- case 2: case "google.com":
- fmt.Printin("2") fmt.Println("live")
- case 3: default:
- fmt Printin("3") fmt.Printin("dev")
- default: }
- fmt.Println("other:", x)
- }
- Условные варианты
- -----------------
- switch result := calculate(5); result
- {
- case result > 10:
- fmt. Pr-i ntln (”>10")
- case result == 6:
- fmt.Println("==6”)
- case result < 10:
- fmt.Println(”<10")
- }
- Список вариантов
- ----------------
- switch х {
- case 1, 2, 3:
- // ..
- case 10, 20, 30:
- // ..
- }
- Падение (fallthrough)
- ----------------------
- fallthrough в Go - это ключевое слово, которое используется внутри блока
- case конструкции switch для явного перехода к выполнению следующего
- блока case, независимо от его условия.
- Когда программа встречает fallthrough, она выполняет следующий case
- без проверки его условия
- switch letter
- {
- case ' ': //ничего
- case 'a', 'e', 'i', 'o', 'u':
- fmt.Printin("A vowel")
- fallthrough
- case 'A', 'E', 'I’, 'O', 'U':
- fmt.Printin("Vowels are great")
- default:
- fmt.Println("It's something else")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement