回圈
Go語言僅支持回圈關鍵字 for
for i := 0; i<5; i++
示例
while 條件回圈
while(n<5)
n := 0
for n < 5 {
n++
fmt.Println(n)
}
while 無限回圈
while(true)
for {
...
}
package loop
import "testing"
func TestWhileLoop(t *testing.T) {
n:=0
for n<5{
t.Log(n)
n++
}
}
輸出
=== RUN TestWhileLoop
--- PASS: TestWhileLoop (0.00s)
loop_test.go:8: 0
loop_test.go:8: 1
loop_test.go:8: 2
loop_test.go:8: 3
loop_test.go:8: 4
PASS
Process finished with exit code 0
if 條件
if condition {
// code to be executed if condition is true
} else {
// code to be executed if condition is false
}
if condition-1 {
// code to be executed if condition-1 is true
} else if condition-2 {
// code to be executed if condition-2 is true
} else {
// code to be executed if condition is false
}
- condition 運算式結果必須為布林值
- 支持變數賦值
if var declaration; condition {
// code to be executed if condition is true
}
示例代碼請訪問: https://github.com/wenjianzhang/golearning
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/54993.html
標籤:Go
