In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to use conditional statements in Go language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use conditional statements in Go language.
1. If...else judgment grammar
The use of grammar is no different from that of other languages.
The sample code is as follows:
/ / judgment statement func panduan (an int) {if a > 50 {fmt.Println ("a > 50")} else if a
< 30 { fmt.Println("a < 30") } else { fmt.Println("a = 30") }} func main() { panduan(120)} 执行结果 a >fifty
2. If nesting syntax
The sample code is as follows
/ / nested judgment func qiantao (b, c uint) {if b > = 100 {b-= 20 if c > b {fmt.Println ("c OK")} else {fmt.Println ("b OK")}
Execution result
C OK
3. Switch statement
The two writing methods do not need to add break.
The sample code is as follows
/ / switch uses func test_switch () {var a uint = 90 var result string switch a {case 90: result = "A" case 80,70,60: result = "B" default: result = "C"} fmt.Printf ("result:% v\ n" Result) switch {case a > 90: result = "A" case a = 80: result = "B" default: result = "C"} fmt.Printf ("result:% v\ n", result)}
Execution result
Result: A
Result: B
Be careful
1. Add a variable after switch, and the following case mainly makes matching judgment. You can also directly use switch {}, case to directly match the results of relational operations.
2. You can choose to match multiple items in case.
4. Type switch statement
Switch statements can use type-switch for type determination, which feels like a very useful syntax.
The sample code is as follows
/ / Test type switchfunc test_type_switch () {var x interface {} x = 1.0 switch I: = x. (type) {case nil: fmt.Printf ("x type =% T\ n", I) case bool String: fmt.Printf ("x type = bool or string\ n") case int: fmt.Printf ("x type = int\ n") case float64: fmt.Printf ("x type = float64\ n") default: fmt.Printf ("unknown\ n")}}
Execution result
X type = float64
Be careful
1. Interface {} can represent any type.
2. Syntax format variables. (type)
5. Use the fallthrough keyword
Use the fallthrough keyword to enforce the contents of the subsequent case statement, whenever the case condition is triggered.
The sample code is as follows
/ / Test fallthroughfunc test_fallthrough () {a: = 1 switch {case a
< 0: fmt.Println("1") fallthrough case a >0: fmt.Println ("2") fallthrough case a
< 0: fmt.Println("3") fallthrough case a < 0: fmt.Println("4") case a >0: fmt.Println ("5") fallthrough case a < 0: fmt.Println ("6") fallthrough default: fmt.Println ("7")}}
Execution result
two
three
four
Be careful
If there is no fallthrough in the case content once the case content is executed down, the case content is stopped.
At this point, I believe you have a deeper understanding of "how to use conditional statements in Go language". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 287
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.