This is a creation in Article, where the information may have evolved or changed.
There is only one loop statement in the go language, which is for:
Here is the first explanation: the Role of =:
: = When the type of the operand on the right is determined, it can be used to do the assignment: = can only be placed inside the function.
of basic for
loops do not have to be ' (') ' (or even force them to use them), they look like C or Java, and ' {} ' is required.
Look at one of the following programs:
Hello project main.go
Package Main
Import"FMT"
FuncMain (){
sum: =1
forsum<
sum+= sum
}
FMT. PRINTLN (SUM)
}
As in C or Java, you can leave the front and back statements empty
Since there is only a for in go to use as a loop, how can our while (i<n) and while (True) be implemented? Take a look at the following program:
Package Main
Import"FMT"
FuncMain (){
sum: =1
forsum<
sum+= sum
}
FMT. PRINTLN (SUM)
}
This is the notation of while (i<n)
Package Main
FuncMain (){
for{
}
}
This is the notation for the dead loop, with no variables behind for