How to use switch case in go
This article mainly introduces how to use switch case in go. It is very detailed and has a certain reference value. Friends who are interested must finish it!
You can have a case with several parameters:
Var I = 0switch I {case 0,1: fmt.Println ("1") case 2:fmt.Println ("2") default: fmt.Println ("def")}
Break effect is available by default. To cancel, add fallthrough:
Var I = 0switch I {case 0: fallthroughcase 1: fmt.Println ("1") case 2:fmt.Println ("2") default: fmt.Println ("def")}
Case can also be an expression:
Var I = 0switch {case I > 0 & & I
< 10: fmt.Println("i >0 and i
< 10")case i >10 & & I
< 20:fmt.Println("i >10 and I < 20 ") default: fmt.Println (" def ")}
Note: the switch case of go has break by default. If you don't need break, add fallthrough**~~.
The above is all the contents of the article "how to use switch case in go". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!