当前位置:网站首页>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
边栏推荐
- pgAdmin 4 v6.11 发布,PostgreSQL 开源图形化管理工具
- Understanding of class
- Summary of Arduino serial functions related to print read
- 【MySQL 12】MySQL 8.0.18 重新初始化
- Comparison of advantages and disadvantages between most complete SQL and NoSQL
- 研究显示乳腺癌细胞更容易在患者睡觉时进入血液
- [set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)
- Project experience sharing: realize an IR Fusion optimization pass of Shengsi mindspire layer
- Sent by mqtt client server of vertx
- TreeMap
猜你喜欢
Custom generic structure
Analysis of the problems of the 10th Blue Bridge Cup single chip microcomputer provincial competition
PAT甲级 1032 Sharing
UA camouflage, get and post in requests carry parameters to obtain JSON format content
The embodiment of generics in inheritance and wildcards
Go language foundation ----- 10 ----- string related operations (operation function, string conversion)
IPv4 address
Introduction of transformation flow
Es writing fragment process
How long is the fastest time you can develop data API? One minute is enough for me
随机推荐
Beginners use Minio
【LeetCode】2. Valid Parentheses·有效的括号
Map interface and method
Go language foundation ----- 06 ----- anonymous fields, fields with the same name
Go language foundation ----- 02 ----- basic data types and operators
【MySQL 14】使用DBeaver工具远程备份及恢复MySQL数据库(Linux 环境)
Talk about floating
The babbage industrial policy forum
Technical dry goods | alphafold/ rosettafold open source reproduction (2) - alphafold process analysis and training Construction
Segment read
项目经验分享:基于昇思MindSpore,使用DFCNN和CTC损失函数的声学模型实现
Go language foundation ----- 03 ----- process control, function, value transfer, reference transfer, defer function
Reconnaissance et détection d'images - Notes
Rabbit MQ message sending of vertx
技术干货|昇思MindSpore算子并行+异构并行,使能32卡训练2420亿参数模型
Analysis of the ninth Blue Bridge Cup single chip microcomputer provincial competition
Hello world of vertx
Lucene skip table
Robots protocol
Chapter VI - Containers