golang中只有一種循環結構——for循环,for循環有以下機種使用方式:
- 由
init statement、condition statement和post statement組成
for i := 0; i < 10; i++ {
// statement
}
- 由
condition statement和post statement組成
for ; i < 10; i++ {
// statement
}
- 由
condition statement組成
// 帶有分號的表示方式
for ; i < 10; {
// statement
}
// 另一種表示方式,像C中while循環
for i < 10 {
// statement
}
- 無限循環
for {
// statement
}