Files
build-web-application-with-…/de/code/src/apps/ch.2.3/panic_and_recover/main.go
2015-08-06 20:35:55 +02:00

32 lines
610 B
Go

// Example code for Chapter 2.3 from "Build Web Application with Golang"
// Purpose: Showing how to use `panic()` and `recover()`
package main
import (
"fmt"
"os"
)
var user = os.Getenv("USER")
func check_user() {
if user == "" {
panic("no value for $USER")
}
fmt.Println("Environment Variable `USER` =", user)
}
func throwsPanic(f func()) (b bool) {
defer func() {
if x := recover(); x != nil {
fmt.Println("Panic message =", x);
b = true
}
}()
f() // if f causes panic, it will recover
return
}
func main(){
didPanic := throwsPanic(check_user)
fmt.Println("didPanic =", didPanic)
}