当前位置:网站首页>go语言-循环语句
go语言-循环语句
2022-07-03 07:36:00 【m0_52339560】
Go语言之循环语句
Go语言的循环语句只有一种,那就是for
for(语句1;语句2;语句3){ /* ......*/ }
语句1为赋值表达式,一般用于给循环控制变量设定初值,语句2为条件控制语句,语句3为赋值表达式,一般用于给循环控制变量进行设置。
使用示例:
for i:=1;i<=10;i++{
println(i)
}
//打印1~10
如上所示,for语句的使用基本与C/C++一致。对于上述for循环中的语句1和语句3是可以省略的。语句1和语句3主要是通过设定的循环控制变量来控制循环次数,我们也可以使用for语句块以外的变量来控制循环,即省略语句1和语句3,只留下条件控制语句(语句2),这时for语句的功能就和C/C++中while语句的功能一致了。
for 条件{ /* ..... */ }
使用示例:
i := 1
for i <= 10{
println(i)
i++
}
//打印1~10
这种for循环的书写格式,可以代替while循环。
- 无限循环
for true{
println("Hello go")
}
//也可以写成如下形式,即省略true
for {
println("Hello go")
}
for-each
通过使用for-each来实现遍历一个可迭代对象的功能。类似于python中的for ... in...。
使用示例:
var arr [3]string = [3]string{
"go", "C", "python"}
for _, value:= range arr{
fmt.Println("Hello ", value)
}
输出结果为:
Hello go
Hello C
Hello python
解释:range会返回两个值,一个是索引,另一个是该索引对应的值。当用不到索引时,可以使用_抛弃改值。
杂谈
由于for循环语句本身便能够实现while循环的特性,go语言舍弃了while语句,体现了go语言的简洁性。
参考资料
- https://www.runoob.com/go/go-for-loop.html
- https://golang.iswbm.com/c01/c01_10.html
边栏推荐
- Technical dry goods | hundred lines of code to write Bert, Shengsi mindspire ability reward
- 密西根大学张阳教授受聘中国上海交通大学客座教授(图)
- Use of generics
- Circuit, packet and message exchange
- The babbage industrial policy forum
- 输入三次猜一个数字
- What did the DFS phase do
- 项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
- Partage de l'expérience du projet: mise en œuvre d'un pass optimisé pour la fusion IR de la couche mindstore
- Analysis of the eighth Blue Bridge Cup single chip microcomputer provincial competition
猜你喜欢

技术干货|昇思MindSpore NLP模型迁移之LUKE模型——阅读理解任务

Common methods of file class

Technical dry goods Shengsi mindspire operator parallel + heterogeneous parallel, enabling 32 card training 242 billion parameter model

The embodiment of generics in inheritance and wildcards

Technical dry goods Shengsi mindspire elementary course online: from basic concepts to practical operation, 1 hour to start!

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

Use of generics

Technical dry goods Shengsi mindspire lite1.5 feature release, bringing a new end-to-end AI experience

Comparison of advantages and disadvantages between most complete SQL and NoSQL

Go language foundation ----- 07 ----- method
随机推荐
Beginners use Minio
圖像識別與檢測--筆記
研究显示乳腺癌细胞更容易在患者睡觉时进入血液
Common architectures of IO streams
项目经验分享:实现一个昇思MindSpore 图层 IR 融合优化 pass
PAT甲级 1028 List Sorting
Technical dry goods | reproduce iccv2021 best paper swing transformer with Shengsi mindspire
How long is the fastest time you can develop data API? One minute is enough for me
Understanding of class
Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
Map interface and method
Collector in ES (percentile / base)
UA camouflage, get and post in requests carry parameters to obtain JSON format content
Es writing fragment process
【LeetCode】2. Valid Parentheses·有效的括号
【MySQL 11】怎么解决MySQL 8.0.18 大小写敏感问题
密西根大学张阳教授受聘中国上海交通大学客座教授(图)
List exercises after class
技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型