Telegram Group & Telegram Channel
Note # 28 fallthrough или “проваливаемся” дальше ⤵️
Go содержит интересное ключевое слово fallthrough, которое на практике встречается довольно редко, его главная задача передать управление следующему оператору case в условных конструкциях switch/case.
Итак поехали 🚴‍♂️:
func main() {
switch 42 {
case 42:
fmt.Println("First case: 42!")
fallthrough
default:
fmt.Println("Second case")
}
}
// First case: 42!
// Second case

Т.е по дефолту в Go switch/case не проваливается дальше - это нужно делать явно используя ключевое слово fallthrough.

Не разрешается использовать в последнем операторе неважно это case или default:
func main() {
switch 42 {
case 42:
fmt.Println("First case: 42!")
default:
fmt.Println("Second case")
fallthrough
}
}
// cannot fallthrough final case in switch

Соответственно такой вариант вполне валидный:
func main() {
switch 42 {
default:
fmt.Println("Second case")
fallthrough
case 42:
fmt.Println("First case: 42!")
}
}
// First case: 42!

Важный момент, "fallthrough" не разрешается использовать в связке с type switch:
func main() {
var pi interface{} = 3.14
switch i := pi.(type) {
case int:
fmt.Printf("%v", i)
fallthrough
default:
fmt.Println("don't know the type")
}
}
// ./prog.go:18:3: cannot fallthrough in type switch

Так же нельзя делать вложенные конструкции которые содержат fallthrough ☔️:
func main() {
switch 42 {
case 42:
fmt.Println("First case: 42!")
if true {
fallthrough
}
case 1:
fmt.Println("First case: 1!")
}
}
// ./prog.go:18:4: fallthrough statement out of place


Useful links:
- ref - https://golang.org/ref/spec#Switch_statements
- wiki https://github.com/golang/go/wiki/Switch



tg-me.com/golang_for_two/53
Create:
Last Update:

Note # 28 fallthrough или “проваливаемся” дальше ⤵️
Go содержит интересное ключевое слово fallthrough, которое на практике встречается довольно редко, его главная задача передать управление следующему оператору case в условных конструкциях switch/case.
Итак поехали 🚴‍♂️:

func main() {
switch 42 {
case 42:
fmt.Println("First case: 42!")
fallthrough
default:
fmt.Println("Second case")
}
}
// First case: 42!
// Second case

Т.е по дефолту в Go switch/case не проваливается дальше - это нужно делать явно используя ключевое слово fallthrough.

Не разрешается использовать в последнем операторе неважно это case или default:
func main() {
switch 42 {
case 42:
fmt.Println("First case: 42!")
default:
fmt.Println("Second case")
fallthrough
}
}
// cannot fallthrough final case in switch

Соответственно такой вариант вполне валидный:
func main() {
switch 42 {
default:
fmt.Println("Second case")
fallthrough
case 42:
fmt.Println("First case: 42!")
}
}
// First case: 42!

Важный момент, "fallthrough" не разрешается использовать в связке с type switch:
func main() {
var pi interface{} = 3.14
switch i := pi.(type) {
case int:
fmt.Printf("%v", i)
fallthrough
default:
fmt.Println("don't know the type")
}
}
// ./prog.go:18:3: cannot fallthrough in type switch

Так же нельзя делать вложенные конструкции которые содержат fallthrough ☔️:
func main() {
switch 42 {
case 42:
fmt.Println("First case: 42!")
if true {
fallthrough
}
case 1:
fmt.Println("First case: 1!")
}
}
// ./prog.go:18:4: fallthrough statement out of place


Useful links:
- ref - https://golang.org/ref/spec#Switch_statements
- wiki https://github.com/golang/go/wiki/Switch

BY 🇺🇦 Go for two :)


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/golang_for_two/53

View MORE
Open in Telegram


🇺🇦 Go на двоих Telegram | DID YOU KNOW?

Date: |

What is Telegram?

Telegram’s stand out feature is its encryption scheme that keeps messages and media secure in transit. The scheme is known as MTProto and is based on 256-bit AES encryption, RSA encryption, and Diffie-Hellman key exchange. The result of this complicated and technical-sounding jargon? A messaging service that claims to keep your data safe.Why do we say claims? When dealing with security, you always want to leave room for scrutiny, and a few cryptography experts have criticized the system. Overall, any level of encryption is better than none, but a level of discretion should always be observed with any online connected system, even Telegram.

Start with a fresh view of investing strategy. The combination of risks and fads this quarter looks to be topping. That means the future is ready to move in.Likely, there will not be a wholesale shift. Company actions will aim to benefit from economic growth, inflationary pressures and a return of market-determined interest rates. In turn, all of that should drive the stock market and investment returns higher.

🇺🇦 Go на двоих from us


Telegram 🇺🇦 Go for two :)
FROM USA