当前位置:网站首页>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 !
边栏推荐
- Log4j2 进阶使用
- What role does "low code" play in enterprise digital transformation?
- linux下新建Mysql数据库并导入sql文件
- [sub matrix quantity statistics] cf1181c flag sub matrix quantity statistics
- Phone number shielding function
- Policy Center > Deceptive Behavior
- ASP. Net core Middleware
- from Crypto. Cipher import AES could not find the solution record with module error
- 交调与互调的区别
- 数数据可视化实战案例(timeline轮播图,streamlit 控件年份 metabase可视化使用教程)2.0
猜你喜欢

halcon变量窗口的图像变量不显示,重启软件和电脑都没用

服务端测试工程师面试经验

分布式机器学习:模型平均MA与弹性平均EASGD(PySpark)

Oracle中的With As 子查询

Message queue ten questions

Policy Center > Google Play‘s Target API Level Policy

【时序数据库InfluxDB】Windows环境下配置InfluxDB+数据可视化,以及使用 C#进行简单操作的代码实例

Advanced C language - pointer 3 - knowledge points sorting

Create a new MySQL database under Linux and import SQL files

Interview experience of service end test engineer
随机推荐
从第三次技术革命看企业应用三大开发趋势
智慧风电:数字孪生 3D 风机智能设备运维
Policy Center > Google Play‘s Target API Level Policy
【Unity UGUI】ScrollRect 动态缩放格子大小,自动定位到中间的格子
Specific steps for installing mysql8.0 on Windows system
Practical cases of data visualization (timeline rotation diagram, streamlit control year metabase visualization tutorial) 2.0
实时渲染和预渲染有什么区别
《网络是怎么样连接的》读书笔记 - 汇总篇
Oracle 导出视图的创建语句
KDD 2022 | 我们离通用预训练推荐模型还有多远?推荐系统的通用序列表示学习模型 UniSRec
Message queue ten questions
互联网研发效能之去哪儿网(Qunar)核心领域DevOps落地实践
How to get the preferential activities for stock account opening? Is online account opening safe?
Deep understanding Net (2) kernel mode 1 Kernel mode construct event event
360 digital, ant group, etc. were selected as member units of the "business security promotion plan" of the Chinese Academy of Communications
容器常用命令
互联网研发效能之去哪儿网(Qunar)核心领域DevOps落地实践
What are the reasons for the errors reported by the Flink SQL CDC synchronization sqlserver
Imeta | Ye Mao / Shi Yu reviewed the dynamic shuttle and ecological function of intracellular and extracellular genes in the environmental microbiome
How the edge computing platform helps the development of the Internet of things