当前位置:网站首页> 详解Go语言中for循环,break和continue的使用
详解Go语言中for循环,break和continue的使用
2022-06-30 15:41:00 【1024问】
基本语法
有始有终的条件循环
带条件的循环
无限循环
数组循环
使用计数器循环
利用range循环
Map循环
string的遍历
Break和Continue
基本语法和C语言同源的语法格式,有始有终的循环,for init; condition; post { }
带条件的while循环,for condition { }
无限循环,for { }
有始有终的条件循环sum := 0for i := 0; i < 10; i++ { sum = sum + i}注意:i变量在循环结束后无法使用
带条件的循环count := 0for count < 10 { fmt.Printf("Current count = %v\n", count) count++}无限循环由于循环不会停止,这里使用break来中断循环,后面还会详细介绍
count := 0 for { fmt.Printf("current count = %v\n", count) count++ if count > 10 { break } }数组循环使用计数器循环类似C语言中的循环,我们可以通过计数器结合数组长度实现对数组的遍历,同时能获取数组索引,如下面例子所示
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]) }}利用range循环利用range可以更容易的进行循环,并且range还能用于slice,channel和map的循环中
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循环在介绍Map时,我们已经尝试用for循环对Map进行遍历,我们再来巩固一下
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) }}如果只想获取key,则可以使用,省略value
for key := range mymap {或者使用_,前面介绍过_无法用于变量,像个占位符
for _, value := range mymap {string的遍历下面的示例是对string类型的遍历,除了普通的字符,对于Unicode字符切分,字符通常是8位的,UTF-8的字符最高可能是32位的
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和Continuecharacter 'G' starts at byte position 0
character 'ő' starts at byte position 1
character '!' starts at byte position 3
与大部分语言一致
Break结束当前循环
Continue开始下一次循环
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")}输出结果
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
不推荐方式
Go中也支持Lable方式,类似Goto,一般不使用
J: for j := 0; j < 5; j++ { for i := 0; i < 10; i++ { if i > 5 { break J } fmt.Println(i) } }到此这篇关于详解Go语言中for循环,break和continue的使用的文章就介绍到这了,更多相关Go语言 for break continue内容请搜索软件开发网以前的文章或继续浏览下面的相关文章希望大家以后多多支持软件开发网!
边栏推荐
- mysql主从配置
- Cesium-1.72 learning (deploy offline resources)
- Policy Center-User Data
- 4.7 type() function query data type
- BYD is more and more like Huawei?
- Kindle倒下,iReader接力
- Policy Center > Device and Network Abuse
- 4.6 floating point number
- Cloud XR, how to help industrial upgrading
- 技不压身,快速入门ETH智能合约开发,带你进入ETH世界
猜你喜欢

Policy Center-User Data

Finally understand science! 200 pictures to appreciate the peak of human wisdom

阿里云oss对象存储跨域设置

Policy Center-Permissions and APIs that Access Sensitive Information

Create a new MySQL database under Linux and import SQL files
![[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment](/img/50/4b7aa2273a64ed531d13ecdbeed6a6.png)
[time series database incluxdb] code example for configuring incluxdb+ data visualization and simple operation with C under Windows Environment

map reduce案例超详细讲解

Alibaba cloud OSS object storage cross domain settings

Practical cases of data visualization (timeline rotation diagram, streamlit control year metabase visualization tutorial) 2.0

Policy Center > Device and Network Abuse
随机推荐
【Leetcode】链表排序(逐步提高时空复杂度)
有意思的鼠标指针交互探究
数数据可视化实战案例(timeline轮播图,streamlit 控件年份 metabase可视化使用教程)2.0
iMeta | 叶茂/时玉等综述环境微生物组中胞内与胞外基因的动态穿梭与生态功能...
波导的种类
Interesting research on mouse pointer interaction
Finally understand science! 200 pictures to appreciate the peak of human wisdom
开源 STM32 USB-CAN项目
优惠券种类那么多,先区分清楚再薅羊毛!
360数科、蚂蚁集团等入选中国信通院“业务安全推进计划”成员单位
topic: Privacy, Deception and Device Abuse
Specific steps for installing mysql8.0 on Windows system
Compulsory national standard for electronic cigarette GB 41700-2022 issued and implemented on October 1, 2022
Policy Center > Misrepresentation
Policy Center-Permissions and APIs that Access Sensitive Information
mysql主从配置
About pickle module - 6 points that beginners must know
阿里云oss对象存储跨域设置
Policy Center-Permissions and APIs that Access Sensitive Information
如何得到股票开户的优惠活动?在线开户安全么?