P3 - ControlStructure

Printing multiple lines

package main

import "fmt"

func main() {

fmt.Println(`1

2

3`)

}

FOR Loop

package main

import "fmt"

func main() {

i := 1

for i <= 10 {

fmt.Println(i)

i = i + 1

}

}

OR

func main() {

for i := 1; i <= 10; i++ {

fmt.Println(i)

}

}

Note : Other programming languages have a lot of different types of loops (while, do, until, foreach, …) but Go only has one that can be used in a variety of different ways.

IF - ELSE Statement

IF - ELSE

if i % 2 == 0 {

// even

} else {

// odd

}

IF -ELSE -IF

if i % 2 == 0 {

// divisible by 2

} else if i % 3 == 0 {

// divisible by 3

} else if i % 4 == 0 {

// divisible by 4

}

SWITCH Statement

switch i {

case 0: fmt.Println("Zero")

case 1: fmt.Println("One")

default: fmt.Println("Unknown Number")

}

Next