当前位置:网站首页>Explain in detail the use of for loop, break and continue in go language
Explain in detail the use of for loop, break and continue in go language
2022-06-30 16:14:00 【1024 Q】
Basic grammar
A conditional cycle that begins and ends
Conditional loop
Infinite loop
Array loop
Use the counter loop
utilize range loop
Map loop
string The traversal
Break and Continue
Basic grammarand C The grammatical form of language homology , A cycle from beginning to end ,for init; condition; post { }
Conditional while loop ,for condition { }
Infinite loop ,for { }
A conditional cycle that begins and endssum := 0for i := 0; i < 10; i++ { sum = sum + i}
Be careful :i Variables cannot be used after the loop ends
Conditional loopcount := 0for count < 10 { fmt.Printf("Current count = %v\n", count) count++}
Infinite loop Because the cycle does not stop , Use here break To break the cycle , It will be introduced in detail later
count := 0 for { fmt.Printf("current count = %v\n", count) count++ if count > 10 { break } }
Array loop Use the counter loop similar C Loops in language , We can traverse the array by combining the counter with the array length , At the same time, it can get the array index , As shown in the following example
package mainimport "fmt"func main() { myarray := []string{"a", "b", "c", "d"} for i := 0; i < len(myarray); i++ { fmt.Printf("Array index is %v, value is %v\n", i, myarray[i]) }}
utilize range loop utilize range It is easier to cycle , also range It can also be used for slice,channel and map In the cycle of
package mainimport "fmt"func main() { myarray := []string{"a", "b", "c", "d"} for index, item := range myarray { fmt.Printf("current index is %v, value is %v\n", index, item) }}
Map loop Introducing Map when , We have tried to use for Cycle pair Map Traversal , Let's consolidate
package mainimport "fmt"func main() { mymap := map[int]string{1 : "a", 2 : "b", 3 : "c"} for key, value := range mymap { fmt.Printf("current key is %v, value is %v\n", key, value) }}
If you just want to get key, You can use , Omit value
for key := range mymap {
Or use _, As mentioned earlier _ Cannot be used for variable , Like a placeholder
for _, value := range mymap {
string The traversal The following example is for string Type traversal , Except for ordinary characters , about Unicode Character segmentation , Characters are usually 8 Bit ,UTF-8 The highest possible character of is 32 Bit
package mainimport "fmt"func main() { mystr := "abc" for pos, char := range mystr { fmt.Printf("character '%c' starts at byte position %d\n", char, pos) } for pos, char := range "Gő!" { fmt.Printf("character '%c' starts at byte position %d\n", char, pos) }}
Break and Continuecharacter 'G' starts at byte position 0
character 'ő' starts at byte position 1
character '!' starts at byte position 3
Consistent with most languages
Break End the current cycle
Continue Start the next cycle
package mainimport "fmt"func main() { for i := 0; i < 10; i++ { if i == 3 { fmt.Printf("For continue at here: %d\n", i) continue } if i > 5 { fmt.Printf("For break at here: %d\n", i) break } fmt.Printf("Current for count: %d\n", i) } fmt.Println("For loop end here")}
Output results
Current for count: 0
Current for count: 1
Current for count: 2
For continue at here: 3
Current for count: 4
Current for count: 5
For break at here: 6
For loop end here
Not recommended
Go China also supports Lable The way , similar Goto, Generally not used
J: for j := 0; j < 5; j++ { for i := 0; i < 10; i++ { if i > 5 { break J } fmt.Println(i) } }
Here is a detailed explanation of Go In language for loop ,break and continue This is the end of the article on the use of , More about Go Language for break continue Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !
边栏推荐
- Policy Center-User Data
- linux下新建Mysql数据库并导入sql文件
- Imeta | Ye Mao / Shi Yu reviewed the dynamic shuttle and ecological function of intracellular and extracellular genes in the environmental microbiome
- Policy Center-Permissions and APIs that Access Sensitive Information
- Generating verification code with sring
- topic: Privacy, Deception and Device Abuse
- Oracle 导出视图的创建语句
- BYD is more and more like Huawei?
- CloudXR如何推动XR的未来发展
- 从第三次技术革命看企业应用三大开发趋势
猜你喜欢
智慧风电:数字孪生 3D 风机智能设备运维
Is your light on? Before you start to solve a problem, you need to know what the "real problem" is
'&lt;', hexadecimal value 0x3C, is an invalid 问题解决
各省GDP可视化案列,附带csv Metabase处理
今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
What is the difference between real-time rendering and pre rendering
Policy Center-Permissions and APIs that Access Sensitive Information
消息队列十连问
优惠券种类那么多,先区分清楚再薅羊毛!
大学生研究生毕业找工作,该选择哪个方向?
随机推荐
Asp.NetCore利用缓存使用AOP方式防止重复提交
【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
Google Play 索引表
今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
Create a new MySQL database under Linux and import SQL files
Deep understanding Net (2) kernel mode 1 Kernel mode construct event event
flink sql cdc 同步sqlserver 报错什么原因啊
CloudXR如何推动XR的未来发展
[download attached] installation and use of penetration test artifact Nessus
Advanced C language - pointer 3 - knowledge points sorting
ADB devices cannot detect the problem of Xiaomi note 3
【算法篇】四种链表总结完毕,顺手刷了两道面试题
LeCun指明下一代AI方向:自主机器智能
Policy Center > Misrepresentation
[CVE-2019-0193] - Apache Solr DataImport 远程命令执行分析
Sword finger offer II 080 Combinatorial backtracking with k elements
linux下修改mysql密码: ERROR 1396 (HY000): Operation ALTER USER failed for ‘root‘@‘localhost‘
Container common commands
互联网研发效能之去哪儿网(Qunar)核心领域DevOps落地实践
Flask-SQLAlchemy----sqlalchemy. exc.InvalidRequestError: SQL expression, column, or mapped e---ORM(9)