Go loop - break and continue

Intro break will break out of a loop. It’s a way to stop looping. continue will move on to the next iteration. Let’s see it in action. Examples Example - break package main import "fmt" func main() { pow := make([]int, 10) for i := range pow { pow[i] = 1 << uint(i) if pow[i] >= 16 { break } } fmt.Println(pow) // [1 2 4 8 16 0 0 0 0 0] } Example - continue package main import "fmt" func main() { pow := make([]int, 10) for i := range pow { if i%2 == 0 { continue } pow[i] = 1 << uint(i) } fmt. [Read More]

Go - for loop

Go has only one looping construct, the for loop. The basic for loop looks as it does in C or Java, except that the ( ) are gone (they are not even optional) and the { } are required. As in C or Java, you can leave the pre and post statements empty. for init; condition; post { } Examples Example - Go loop for j := 7; j <= 9; j++ { fmt. [Read More]
loop  for 

Go Conditionals - switch

Intro In addition to the switch keyword, a switch statement has cases. The switch statement switches on some case. Example - No break required Compared to other programming languages (such as C), Go’s switch-case statement doesn’t need explicit “break”, and not have fall-though characteristic. Take the following code as an example: package main import ( "fmt" ) func checkSwitch(val int) { switch val { case 0: case 1: fmt.Println("The value is: ", val) } } func main() { checkSwitch(0) checkSwitch(1) } The output is: [Read More]

Go Conditinals - if else statement

if Statement Simple if syntax The if statement looks as it does in C or Java, except that the ( ) are gone and the { } are required. Like for, the if statement can start with a short statement to execute before the condition. Variables declared by the statement are only in scope until the end of the if. Variables declared inside an if short statement are also available inside any of the else blocks. [Read More]