Branching
If statements
A simple if statement:
package main
import "fmt"
func main() {
height := 180
if height < 200 {
fmt.Println("Less than two meters.")
}
fmt.Println("After if statement.")
}
With and else statement:
package main
import "fmt"
func main() {
height := 200
if height <= 200 {
fmt.Println("Less than or equal to two meters.")
} else {
fmt.Println("More than two meters.")
}
fmt.Println("After if statement.")
}
With multiple if conditions:
package main
import "fmt"
func main() {
height := 400
if height <= 200 {
fmt.Println("Less than or equal to two meters.")
} else if height <= 300 {
fmt.Println("Less than or equal to three meters.")
} else {
fmt.Println("More than three meters.")
}
fmt.Println("After if statement.")
}
You can include the initializer, for example:
package main
import "fmt"
func main() {
if height := 180; height < 200 {
fmt.Println("Less than two meters.")
}
fmt.Println("After if statement.")
}
Switch
Example switch statement:
package main
import "fmt"
func main() {
switch id := 1; id {
case 1:
fmt.Println("Case one")
case 2:
fmt.Println("Case two")
default:
fmt.Println("Default case")
}
fmt.Println("After switch statement.")
}
Another example which includes an infinite loop which is labeled loop providing a means to break from the for loop:
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
userInput := bufio.NewReader(os.Stdin)
loop:
for {
fmt.Println("Pick an option:")
fmt.Println("1) Option one")
fmt.Println("2) Option two")
fmt.Println("q) Quit")
choice, _ := userInput.ReadString('\n')
switch strings.TrimSpace(choice) {
case "1":
fmt.Println("You picked option one")
case "2":
fmt.Println("You picked option two")
case "q":
break loop
default:
fmt.Println("Option unknown")
}
}
}
Example fo a logical switch statement, where the condition is looking for true or false.
package main
import (
"fmt"
)
func main() {
switch number := 42; {
case number < 10:
fmt.Println("Number is less than ten")
case number < 50:
fmt.Println("Number is less than fifty")
default:
fmt.Println("Number is fifty or more")
}
}
Deferred functions
Deferred functions taken advantage of the window between when a function exits and to program returns its focus to the calling point of said function.
A great example of deferred functions are when opening and closing a database connection.
Deferred functions operate with a first-in last-out behavior, when the main() function finishes in the following example, the two deferred functions will execute:
package main
import (
"fmt"
)
func main() {
fmt.Println("Main function 1")
defer fmt.Println("Deferred function 1")
fmt.Println("Main function 2")
defer fmt.Println("Deferred function 2")
}
Output:
Panic and recovery
Panic and recovery leverage the deferred function.
Example:
package main
import (
"fmt"
)
func main() {
dividend, divisor := 10, 5
fmt.Printf("%v divided by %v is %v\n", dividend, divisor, divide(dividend, divisor))
dividend, divisor = 10, 0
fmt.Printf("%v divided by %v is %v\n", dividend, divisor, divide(dividend, divisor))
}
func divide(dividend, divisor int) int {
defer func() {
if message := recover(); message != nil {
fmt.Println(message)
}
}()
return dividend / divisor
}
Goto statements
It's likely you will never use a goto statement in your go programming. They use labels.
A goto statement can leave a block, that is a block of code between curly braces. You can't jump after variable declarations, else they wouldn't get declared. You can't jump into another code black.