Looping in Go
Looping in Go is fairly similar to any other language that you may of seen in the past, with some slight variations that are particular to Go.
The init statement, i := 0, runs.The condition, i < 5, is evaluated.If it’s true, the loop body executes,otherwise the loop terminates.The post statement, i++, executes.Back to step 2.
Breaking it down step by step:
The condition, powerOf < 5, is evaluated.If it’s true, the loop body executes,Otherwise the loop terminates.Back to step 1.
Looping over elements in slices, arrays, maps, channels and strings is done using the range key word, which is far more efficient then writing a for loop.
The step by step process of a for loop with range is:
The range expression, strings, is evaluated once before beginning the loop. The iteration values are assigned to the respective iteration variables, i and s, as in an assignment statement. The second iteration variable is optional. If a slice or map is nil, the number of iterations is 0.
That’s the basics of looping in Go, hopefully this will be helpful as you enter into the world of Go.