2. GO Loops
for loop
● Only looping construct
● Three components
○ Init statement : executed at start of loop
○ Condition expression : executed before every iteration
○ Post statement : executed ar end of every iteration
● No parentheses for three conditions, function body inside { }
● Init and post statements are optional
3. Go for loops example
package main
import "fmt"
func main() {
sum := 0
for i := 0; i < 10; i++ {
sum += i
}
fmt.Println(sum)
}
4. Go for loops example
package main
import "fmt"
func main() {
sum := 1
for ; sum < 10; {
sum += sum
}
fmt.Println(sum)
}
5. GO Conditions
If
● No parentheses for conditions, body inside { }
● Short expression can be used in go, having scope in “if” only
● If can be paired with else as well, else body inside { }
7. GO iteration
range
● iterates over elements in a variety of data structures
● range on arrays and slices provides both the index and value for each entry
● range on map iterates over key/value pairs
● range can also iterate over just the keys of a map
● range on strings iterates over Unicode code points
○ first value is the starting byte index of the rune and the second the rune
itself
8. References
● “The Way to Go: A Thorough Introduction to the Go Programming Language”,
● https://tour.golang.org/moretypes/
● https://gobyexample.com/
●