package main import "os" import "fmt" func main() { var user = os.Getenv("USER") if user == "" { panic("The USER environment variable is not set.") } fmt.Println("continue exec") }
package main import "fmt" func f1() { fmt.Println("This is f1()") } func f2() { defer func() { if err := recover(); err != nil { fmt.Println("Exception has been caught.") } }() fmt.Println("This is f2()") panic(1) } func f3() { fmt.Println("This is f3()") } func main() { f1() f2() f3() }
package main import "fmt" func fn(n int) int { defer func() { n++ fmt.Println("3st:", n) }() defer func() { n++ fmt.Println("2st:", n) }() defer func() { n++ fmt.Println("1st:", n) }() return n //沒有作任何事情 } func main() { fmt.Println("函數返回值:", fn(0)) }