In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to use the loop sentence of the Go language". In the daily operation, I believe that many people have doubts about how to use the loop sentence of the Go language. The editor consulted all kinds of materials and sorted out the simple and easy-to-use operation methods. I hope it will be helpful to answer the doubt of "how to use the loop sentence of Go language". Next, please follow the editor to study!
1. For cycle
The way of writing is basically the same as that of other languages, except that without the while loop, for is used instead of while.
The sample code is as follows
/ / for loop func loop1 () {sum: = 0 for I: = 0; I
< 100; i++ { sum += i } fmt.Printf("sum = %d\n", sum) // 和while循环一样 sum1 := 3 for sum1 0 { if a >4 {break} averse + b-= 3} fmt.Printf ("aversion% ddirection billing% d\ n", a, b) var c int = 0 var d int = 30 for I: = 0; I
< 10; i++ { c++ for j := 0; j < 10; j++ { if d < 0 { break } d-- } } fmt.Printf("c=%d,d=%d\n", c, d) c = 0 d = 30loop1: for i := 0; i < 10; i++ { c++ for j := 0; j < 10; j++ { if d < 0 { break loop1 } d-- } } fmt.Printf("c=%d,d=%d\n", c, d)} 执行结果 a=5,b=15 c=10,d=-1 c=4,d=-1 注意 1、和java一样,在多重嵌套循环里面,如果不对循环做标识,break只会打断最近的循环。 4、continue的使用 和break基本一致。 样例代码如下 // continuefunc loop4() { var c int = 0 var d int = 30 for i := 0; i < 10; i++ { c++ for j := 0; j < 10; j++ { d-- if d < 0 { continue } } } fmt.Printf("c=%d,d=%d\n", c, d) c = 0 d = 30loop1: for i := 0; i < 10; i++ { c++ for j := 0; j < 10; j++ { d-- if d < 0 { continue loop1 } } } fmt.Printf("c=%d,d=%d\n", c, d)} 执行结果 c=10,d=-70 c=10,d=-7 5、goto的使用 goto是个有意思的关键字,可以无条件转移到某一行,有点debug的味道。 样例代码如下 //gotofunc loop5() { m := 0loop1: for m < 20 { fmt.Printf("loop1 ->% d\ n ", m) if m > 3 {break loop1} I: = 0 loop2: for I
< 10 { i++ if i == 5 { goto loop2 } fmt.Printf("loop2 ->% d\ n ", I)} masked +}}
Execution result
Loop1-> 0
Loop2-> 1
Loop2-> 2
Loop2-> 3
Loop2-> 4
Loop2-> 6
Loop2-> 7
Loop2-> 8
Loop2-> 9
Loop2-> 10
Loop1-> 1
Loop2-> 1
Loop2-> 2
Loop2-> 3
Loop2-> 4
Loop2-> 6
Loop2-> 7
Loop2-> 8
Loop2-> 9
Loop2-> 10
Loop1-> 2
Loop2-> 1
Loop2-> 2
Loop2-> 3
Loop2-> 4
Loop2-> 6
Loop2-> 7
Loop2-> 8
Loop2-> 9
Loop2-> 10
Loop1-> 3
Loop2-> 1
Loop2-> 2
Loop2-> 3
Loop2-> 4
Loop2-> 6
Loop2-> 7
Loop2-> 8
Loop2-> 9
Loop2-> 10
Loop1-> 4
Be careful
When testing, I still encounter the problem of an endless loop, which must be well understood before I can use it, otherwise it is easier to use it.
At this point, the study of "how to use cyclic sentences in Go language" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 0
*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.