Looping in Go
Posted on January 31, 2020
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.
I finished the first part of the nanodegree a few days ago and decided to give an indepth review of what is covered, what you learn and what is completed by the end of it.
When you first start thinking about becoming a developer, one of the things you might hear is you need to be a math(s) wizz. I am here to tell you that a small number of developers need the mathematics outside of algebra.