网站首页 > 文章精选 正文
基本语法
- o 和C语言同源的语法格式,有始有终的循环,for init; condition; post { }
- o 带条件的while循环,for condition { }
- o 无限循环,for { }
有始有终的条件循环
sum := 0
for i := 0; i < 10; i++ {
sum = sum + i
}
注意:i变量在循环结束后无法使用
带条件的循环
count := 0
for 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 main
import "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 main
import "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 main
import "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 main
import "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"o!" {
fmt.Printf("character '%c' starts at byte position %d\n", char, pos)
}
}
character 'G' starts at byte position 0
character '"o' starts at byte position 1
character '!' starts at byte position 3
Break和Continue
与大部分语言一致
- o Break结束当前循环
- o Continue开始下一次循环
package main
import "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")
}
- o 输出结果
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
不推荐方式
- o Go中也支持Lable方式,类似Goto,一般不使用
J: for j := 0; j < 5; j++ {
for i := 0; i < 10; i++ {
if i > 5 {
break J
}
fmt.Println(i)
}
}
猜你喜欢
- 2025-07-14 Go并发编程之WaitGroup(go语言 并发)
- 2025-07-14 golang企业微信告警(企业微信告警推送)
- 2025-07-14 Go语言Context包:最常用包之一的使用指南
- 2025-07-14 Go语言零到一:动态数组——切片(go语言的切片)
- 2025-07-14 2025-06-26:转换数组。用go语言,给你一个整数数组 nums,它被视
- 2025-07-14 go sync.Pool简介(go system)
- 2025-07-14 2025-07-13:统计特殊子序列的数目。用go语言,给定一个只包含正
- 2025-07-14 Go语言数据库编程:GORM 的基本使用
- 2025-07-14 2025-06-28:长度可被 K 整除的子数组的最大元素和。用go语言,给
- 2025-07-14 Go语言零到一:初识变量(go语言示例)
- 最近发表
- 标签列表
-
- newcoder (56)
- 字符串的长度是指 (45)
- drawcontours()参数说明 (60)
- unsignedshortint (59)
- postman并发请求 (47)
- python列表删除 (50)
- 左程云什么水平 (56)
- 编程题 (64)
- postgresql默认端口 (66)
- 数据库的概念模型独立于 (48)
- 产生系统死锁的原因可能是由于 (51)
- 数据库中只存放视图的 (62)
- 在vi中退出不保存的命令是 (53)
- 哪个命令可以将普通用户转换成超级用户 (49)
- noscript标签的作用 (48)
- 联合利华网申 (49)
- swagger和postman (46)
- 结构化程序设计主要强调 (53)
- 172.1 (57)
- apipostwebsocket (47)
- 唯品会后台 (61)
- 简历助手 (56)
- offshow (61)
- mysql数据库面试题 (57)
- fmt.println (52)