-
Notifications
You must be signed in to change notification settings - Fork 2
/
func_recover.go
52 lines (48 loc) · 1.54 KB
/
func_recover.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
// Recover function is used to handle panic.
// Only useful in deferred functions.
// Current function will not attempt to continue, but higher functions in call stack will.
// Recover function always called inside the deferred function not in the normal function.
// If we call the Recover function as normal func then it will be not stop panicking.
// In deferred function, it will be perform at the end of program execution. Because features of defer func.
// To take over the panic error results(error msg,goroutine traces & exit with a non zero status), we used recover function.
package main
import "fmt"
// func main(){
// anonymous func
// defer func(){
// str := recover()
// fmt.Println(str)
// }()
// panic("PANIC")
// }
func point() {
if a := recover(); a != nil{
fmt.Println("Recover function:- ",a)
}
}
// func
func book(lang *string, name *string){
// called as a normal function
// point()
// called with deferred func
defer point()
// when lang section is nil it will be panic
if lang == nil{
panic("Error: Language section cannot be empty!!")
}
// when name section is nil it will be panic
if name == nil {
panic("Error: Name section cannot be empty!!")
}
// when value is not nil then it will be print
fmt.Printf("Book language: %s\n Book name: %s\n ", *lang, *name)
fmt.Println("Deferred function defined with normal function.")
}
// main function
func main(){
// bookName := "Rapid Fire"
langName := "English"
// book(&langName,&bookName)
book(&langName,nil)
fmt.Println("Main function.")
}