March 2019
Intermediate to advanced
336 pages
9h 9m
English
Panic, defer, and recover are used to handle complex errors. The last returned variable in a function is used as an error. The following code snippet is an example of this:
//main package has examples shown// in Go Data Structures and algorithms bookpackage main// importing fmt and errors packagesimport( "fmt" "errors")//First Func methodfunc FirstFunc(v interface{}) (interface{}, error) { var ok bool if !ok { return nil, errors.New("false error") } return v, nil}//SecondFunc methodfunc SecondFunc() { defer func() { var err interface{} if err = recover(); err != nil { fmt.Println("recovering error ", err) } }() var v interface{} v = struct{}{} var err error if _, err = FirstFunc(v); err != nil { panic(err) } fmt.Println("The ...Read now
Unlock full access