Go中的无限循环实例

1,715 阅读1分钟

要在Go中定义一个无限循环,在许多不同的语言中也被称为 "while true "循环,你需要使用for 语句。传统的for 循环的形式是。

for initialization; condition; post-condition {
...
}

但当你省略了初始化、条件和后置条件语句时,你会得到经典的 "while true "无限循环。

for {
...
}

Go中没有while 这个关键字,所以所有的 "while "循环都可以用for 这个关键字来创建

例子

package main
import (
"fmt"
"time"
)
func main() {
for {
fmt.Println("https://gosamples.dev is the best")
time.Sleep(1 * time.Second)
}
}