注意:使用goto語句是高度勸阻的在任何編程語言,因為它使得難以跟蹤程序的控制流程,使程序難以理解,難以修改。使用一個goto任何程序可以改寫,以便它不需要goto。
goto label;
..
.
label: statement;
package main
import "fmt"
func main() {
/* local variable definition */
var a int = 10
/* do loop execution */
LOOP: for a 20 {
if a == 15 {
/* skip the iteration */
a = a + 1
goto LOOP
}
fmt.Printf("value of a: %d\n", a)
a++
}
}
一個循環(huán)變得無限循環(huán),如果條件永遠不會為假。 for循環(huán)是傳統(tǒng)上用于此目的。由于沒有形成for循環(huán)是必需的三個表達式中,可以通過離開條件,使用空表達式或傳遞true給它形成一個死循環(huán)。
package main
import "fmt"
func main() {
for true {
fmt.Printf("This loop will run forever.\n");
}
}