-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_if_operator.go
44 lines (37 loc) · 1.15 KB
/
08_if_operator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//Desicion making. Logical if operator.
package main
import "fmt"
func main() {
// In programming languages conditions are used to control flow of the program
// Syntax: if condition { executed only if condition is truthful }
// Optionally with 'else' branch
// if condition { executed only if condition is truthful } else { executed only if condition is NOT truthful }
if true {
fmt.Println("Truth is Truth. Always.")
} else {
fmt.Println("Truth isn't Truth, The Ministry of Truth said.")
}
// variables valid only within the scope of the execution blocks
if colour, err := isFavouriteColour(""); err != nil {
fmt.Println(err)
} else {
fmt.Println(colour)
}
// variables valid within the scope of the enclosing function
colour, err := isFavouriteColour("orange")
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Is orange Josef's favourite colour: %t\n", colour)
}
func isFavouriteColour(colour string) (bool, error) {
if colour == "" {
return false, fmt.Errorf("Please send us a colour :(")
}
if colour == "orange" {
return true, nil
}
return false, nil
}
//Task: Find if a number is odd or even and print out the result.