Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use switch case in go

Shulou Source: shulou.com Published: 2022-06-02 11:22:27 11月02日 Update

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!

Tags: Content articles values interests parameters guys partners effects more knowledge industry expressions information channels channels references help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Xiaomi Docker Apple Linux