-
Notifications
You must be signed in to change notification settings - Fork 0
/
recover05.go
57 lines (41 loc) · 1.48 KB
/
recover05.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
45
46
47
48
49
50
51
52
53
54
55
56
/* Alta3 Research | RZFeeser
Refer - Greenskeeper routine with panic(), defer, and recover() */
package main
import (
"fmt"
)
func waterLawn() {
defer func() { // this must be named func()
if r := recover(); r != nil {
fmt.Println("Error detected and recovered in the watering system (waterLawn):", r)
}
}()
fmt.Println("Begin watering system.")
water(5) // we will imagine that there is a bug in our program
// a value of 3 or higher will crash our program
fmt.Println("Watering system finished.")
}
func water(g int) {
fmt.Println("Starting", g, "watering systems.")
for i := 1; i < g+1; i++ {
fmt.Println("Turning on sprinkler at station: ", i)
defer fmt.Println("Turning off sprinkler at station: ", i)
// this is the imaginary "bug" in our function
// it only happens if i becomes equal to 3
if i == 3 {
fmt.Println("Panicking!")
// panic requires input of a single sting
panic(fmt.Sprintf("Error occurred on iteration %v with function input %v", i, g))
}
}
}
func main() {
waterLawn()
// two imaginary functions we might still want to have run
// even through waterLawn() recovers from a failue
// measureSoilPH()
// measureSoilTemp()
fmt.Println("Begin measuring soil pH.")
fmt.Println("Begin measuring soil temperature.")
fmt.Println("Completed Golf Course Greenskeeper Routine")
}