Loops
Loops are simplified in Go
You only have the “for loop”
Infinite Loop
for {
//anything inside will run infinitely
}
//same as
for true {
//anything inside will run infinitely
}
for-each loop
Iterating over a list
range
range iterates over elements for different data structures (so not only arrays and
slices)
for arrays and slices, range provides the index and value for each element
In Go, like for each loop, if you don’t want to use the <key>, you will
leave it blank. Then go will give error. To remove the error and not
use the <key>, use _ instead.
for <key>, <value> := range <container>{
Loops 1
//This can be written as
for _, <value> := range <container>{
Now go ignores the <key> and also does not gives error.
Loops 2