ta_shuo

Go by Example - For

翻译自 https://gobyexample.com/

Go by Example

Go is an open source programming language designed for building simple, fast, and reliable software.

Go by Example is a hands-on introduction to Go using annotated example programs. Check out the first example or browse the full list below.

Go by Example: For

for是golang唯一的循环结构。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package main
import "fmt"
func main() {
// 最常见的形式,只有一个条件。类似于PHP中的while循环
i := 1
for i <= 3 {
fmt.Println(i)
i = i + 1
}
// 经典的for循环
for j := 7; j <= 9; j++ {
fmt.Println(j)
}
// 不带任何条件的for循环,类似于PHP中的while(true)循环。除非从函数内部break或return,否则会一直循环下去。
for {
fmt.Println("loop")
break
}
// continue关键字,同PHP。停止此次循环的执行,直接进入下一次循环。
for n:= 0; n <= 5; n++ {
if n%2 == 0 {
continue
}
fmt.Println(n)
}
}
1
2
3
4
5
6
7
8
9
10
11
$ go run for.go
1
2
3
7
8
9
loop
1
3
5

原文链接:Go by Example: For