π Edit page
β Add page
Errors
Handle errors in Go
Check function
Save yourself doing 3-line checks for err != nil
all over and use a check
function,
func check(e error) {
if e != nil {
panic(e)
}
}
Usage:
f, err := os.Open("/tmp/foo.txt")
check(err)
Panic
From Effective Go.
real library functions should avoid panic.
If the problem can be masked or worked around, itβs always better to let things continue to run rather than taking down the whole program. One possible counterexample is during initialization: if the library truly cannot set itself up, it might be reasonable to panic, so to speak.
var user = os.Getenv("USER")
func init() {
if user == "" {
panic("no value for $USER")
}
}
Recover
recover is only useful inside deferred functions.
One application of recover is to shut down a failing goroutine inside a server without killing the other executing goroutines.