当前位置:网站首页>Go language - loop statement
Go language - loop statement
2022-07-03 07:40:00 【m0_ fifty-two million three hundred and thirty-nine thousand fi】
Go Circular statements of language
Go There is only one kind of circular statement in language , That's it for
for( sentence 1; sentence 2; sentence 3){ /* ......*/ }
sentence 1 Assign an expression to , It is generally used to set the initial value of the cycle control variable , sentence 2 For conditional control statements , sentence 3 Assign an expression to , It is generally used to set the loop control variable .
Examples of use :
for i:=1;i<=10;i++{
println(i)
}
// Print 1~10
As shown above ,for The use of statements is basically the same as C/C++ Agreement . For the above for In the loop sentence 1 and sentence 3 It can be omitted . sentence 1 And statement 3 Mainly through the set cycle control variables to control the number of cycles , We can also use for Variables outside the statement block to control the loop , I.e. omitted sentence 1 and sentence 3, Only conditional control statements are left ( sentence 2), At this time for The function of the statement is the same as C/C++ in while The functions of the statements are consistent .
for Conditions { /* ..... */ }
Examples of use :
i := 1
for i <= 10{
println(i)
i++
}
// Print 1~10
such for Circular writing format , Can replace while loop .
- Infinite loop
for true{
println("Hello go")
}
// It can also be written as follows , I.e. omitted true
for {
println("Hello go")
}
for-each
By using for-each To realize the function of traversing an iteratable object . Be similar to python Medium for ... in....
Examples of use :
var arr [3]string = [3]string{
"go", "C", "python"}
for _, value:= range arr{
fmt.Println("Hello ", value)
}
The output is :
Hello go
Hello C
Hello python
explain :range Two values will be returned , One is the index , The other is the value corresponding to the index . When indexes are not available , have access to _ Discard change value .
gossip
because for The loop statement itself can be implemented while The nature of the cycle ,go Language abandoned while sentence , Embodies the go The simplicity of language .
Reference material
- https://www.runoob.com/go/go-for-loop.html
- https://golang.iswbm.com/c01/c01_10.html
边栏推荐
- opensips与对方tls sip trunk对接注意事项
- The concept of C language pointer
- Application of pigeon nest principle in Lucene minshouldmatchsumscorer
- 技术干货|昇思MindSpore初级课程上线:从基本概念到实操,1小时上手!
- Lucene merge document order
- Use of generics
- Web router of vertx
- 为什么说数据服务化是下一代数据中台的方向?
- Lombok -- simplify code
- 技术干货|昇思MindSpore可变序列长度的动态Transformer已发布!
猜你喜欢

Go language foundation ----- 11 ----- regular expression

为什么说数据服务化是下一代数据中台的方向?

The concept of C language pointer

Use of generics

VMWare网络模式-桥接,Host-Only,NAT网络

Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition

Go language foundation ----- 02 ----- basic data types and operators

Summary of Arduino serial functions related to print read

Go language foundation ----- 10 ----- string related operations (operation function, string conversion)

Paper learning -- Study on the similarity of water level time series of Xingzi station in Poyang Lake
随机推荐
PAT甲级 1031 Hello World for U
Understanding of class
lucene scorer
Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
The concept of C language pointer
哪一刻你才发现青春结束了
VMware network mode - bridge, host only, NAT network
【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
Use of file class
Go language foundation ----- 02 ----- basic data types and operators
Responsive MySQL of vertx
Go language foundation ----- 18 ----- collaboration security, mutex lock, read-write lock, anonymous lock, sync Once
項目經驗分享:實現一個昇思MindSpore 圖層 IR 融合優化 pass
【MindSpore论文精讲】AAAI长尾问题中训练技巧的总结
Hnsw introduction and some reference articles in lucene9
Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward
Go language foundation ------ 14 ------ gotest
Logging log configuration of vertx
OSI knowledge sorting
Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition