GO新手入门:for循环
Github地址:https://github.com/zoulee24/GO_NoobNote
GO的for循环有着python和c++的特性
重要
变量自增(代码第13行)必须独立出来写,不能和其他语句写在一起
package _06
import "fmt"
func main() {
count := 0
//for循环格式1
for true {
if count > 10 {
break
}
//报错syntax error: unexpected ++, expecting comma or )
//fmt.Println(count++)
fmt.Println(count)
count++
}
fmt.Println("end1\n")
count = 0
//for循环格式2
for count < 10 {
fmt.Println(count)
count++
}
fmt.Println("end2\n")
count = 0
//for循环格式3
for i := 0; i < 10; i++ {
fmt.Println(i)
}
fmt.Println("end3\n")
//for循环格式4
for ; count < 10; {
fmt.Println(count)
count++
}
fmt.Println("end4\n")
//for循环格式5
numbers := [6]int{1, 2, 3, 4, 5}
for key, val := range numbers {
fmt.Print("key: %d val: %d\n", key, val)
}
fmt.Println("end5\n")
}
本文是GO语言新手入门系列的一部分,主要介绍了GO的for循环语法,包括四种基本格式:无限循环、条件循环、计数循环和range遍历。强调了变量自增必须独立写,并通过实例展示了每种循环的应用。此外,还涵盖了for循环在遍历数组时的使用方法。
2388

被折叠的 条评论
为什么被折叠?



