golang里面没有while关键字,可以用for+break实现。
i:=0
for {
do something
i++
if i <= 10 { break }
}//for
与java里面while等价。
int i = 0;
while(i<10){// notice there is only <
do something
i--; }//while
综合案例
//输出10次hello,world(使用类似while循环形式,先判断后做)
func jobWhile() {
var count = 0
for {
if count >= 10 {
break //如果count>=10则退出
}
fmt.Println("hello,world", count)
count++
}
}
//模拟do……while实现输出10次hello,world(先做后判断)
func jobDowhile(){
var i = 0
for{
fmt.Println("hello,world",i)
i++
if(i>=10){
break
}
}
}