当前位置:网站首页>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
边栏推荐
- 截图工具Snipaste
- [coppeliasim4.3] C calls UR5 in the remoteapi control scenario
- 【CoppeliaSim4.3】C#调用 remoteApi控制场景中UR5
- Chapter VI - Containers
- Technical dry goods Shengsi mindspire innovation model EPP mvsnet high-precision and efficient 3D reconstruction
- Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
- 为什么说数据服务化是下一代数据中台的方向?
- Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
- 【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
- Go language foundation ----- 02 ----- basic data types and operators
猜你喜欢
Application of pigeon nest principle in Lucene minshouldmatchsumscorer
技术干货|百行代码写BERT,昇思MindSpore能力大赏
Technology dry goods | luxe model for the migration of mindspore NLP model -- reading comprehension task
最全SQL与NoSQL优缺点对比
技术干货|利用昇思MindSpore复现ICCV2021 Best Paper Swin Transformer
Robots protocol
Analysis of the problems of the 11th Blue Bridge Cup single chip microcomputer provincial competition
How long is the fastest time you can develop data API? One minute is enough for me
PAT甲级 1032 Sharing
Topic | synchronous asynchronous
随机推荐
Go language foundation ----- 07 ----- method
Responsive MySQL of vertx
Application of pigeon nest principle in Lucene minshouldmatchsumscorer
IPv4 address
Beginners use Minio
Analysis of the problems of the 12th Blue Bridge Cup single chip microcomputer provincial competition
The concept of C language pointer
Collector in ES (percentile / base)
Sent by mqtt client server of vertx
GoLang之结构体
HarmonyOS第三次培训笔记
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
Go language foundation ----- 11 ----- regular expression
Logging log configuration of vertx
How long is the fastest time you can develop data API? One minute is enough for me
【MySQL 12】MySQL 8.0.18 重新初始化
输入三次猜一个数字
VMware network mode - bridge, host only, NAT network
OSI knowledge sorting
Use of file class