error接口
Go语言引入了一个关于错误处理的标准模式,即error接口,该接口的定义如下:
type error interface {
Error() string
}
对于大多数函数,如果要返回错误,大致上都可以定义为如下模式,将error作为多种返回
值中的最后一个,但这并非是强制要求:
func Foo(param int)(n int, err error) {
// ...
}
调用时的代码建议按如下方式处理错误情况:
n, err := Foo(0)
if err != nil {
// 错误处理
} else {
// 使用返回值n
}
使用自定义的error类型:
package main
import "errors"
import "fmt"
// By convention, errors are the last return value and
// have type `error`, a built-in interface.
func f1(arg int) (int, error) {
if arg == 42 {
// `errors.New` constructs a basic `error` value
// with the given error message.
return -1, errors.New("can't work with 42")
}
// A nil value in the error position indicates that
// there was no error.
return arg + 3, nil
}
// It's possible to use custom types as `error`s by
// implementing the `Error()` method on them. Here's a
// variant on the example above that uses a custom type
// to explicitly represent an argument error.
type argError struct {
arg int
prob string
}
func (e *argError) Error() string {
return fmt.Sprintf("%d - %s", e.arg, e.prob)
}
func f2(arg int) (int, error) {
if arg == 42 {
// In this case we use `&argError` syntax to build
// a new struct, supplying values for the two
// fields `arg` and `prob`.
return -1, &argError{arg, "can't work with it"}
}
return arg + 3, nil
}
func main() {
// The two loops below test out each of our
// error-returning functions. Note that the use of an
// inline error check on the `if` line is a common
// idiom in Go code.
for _, i := range []int{7, 42} {
if r, e := f1(i); e != nil {
fmt.Println("f1 failed:", e)
} else {
fmt.Println("f1 worked:", r)
}
}
for _, i := range []int{7, 42} {
if r, e := f2(i); e != nil {
fmt.Println("f2 failed:", e)
} else {
fmt.Println("f2 worked:", r)
}
}
// If you want to programmatically use the data in
// a custom error, you'll need to get the error as an
// instance of the custom error type via type
// assertion.
_, e := f2(42)
if ae, ok := e.(*argError); ok {
fmt.Println(ae.arg)
fmt.Println(ae.prob)
}
}
执行结果:
f1 worked: 10
f1 failed: can't work with 42
f2 worked: 10
f2 failed: 42 - can't work with it
42
can't work with it
defer
一个函数中可以存在多个defer语句,因此需要注意的是,defer语句的调用是遵照先进后出的原则,即最后一个defer语句将最先被执行。
func ReadWrite() bool {
file.Open("file")
defer file.Close() //file.Close() 被添加到了defer 列表
// 做一些工作
if failureX {
return false //Close() 现在自动调用
}
if failureY {
return false //这里也是
}
return true
}
也可以使用在defer后加一个匿名函数的做法:
defer func() {
// 做你复杂的清理工作
} ()
还需要注意的一点是,defer需要先被加载之后才会生效,即如果函数返回前没执行到defer语句则不会触发,参考下面的例子。
恐慌(Panic)和恢复(Recover)
Panic
是一个内建函数,可以中断原有的控制流程,进入一个令人恐慌的流程中。当函数F 调用panic,函数F 的执行被中断,并且F 中的延迟函数会正常执行,然后F 返回到调用它的地方。在调用的地方, F 的行为就像调用了panic。这一过程继续向上,直到程序崩溃时的所有goroutine 返回。恐慌可以直接调用panic 产生。也可以由运行时错误产生,例如访问越界的数组。
Recover
是一个内建的函数,可以让进入令人恐慌的流程中的goroutine 恢复过来。recover 仅在延迟函数中有效。在正常的执行过程中,调用recover 会返回nil 并且没有其他任何效果。如果当前的goroutine 陷入恐慌,调用recover 可以捕获到panic 的输入值,并且恢复正常的执行。
package main
import (
"fmt"
)
func main() {
//如果在这个位置添加panic,则不会触发defer
//panic("a problem")
//退出之前执行延迟函数
defer func() {
if r := recover(); r != nil {
fmt.Println(r)//输出错误信息a problem
fmt.Println("recover a problem")
}
}()
// 发现错误,程序退出
panic("a problem")
//下面的代码不会执行到
fmt.Println("no problem")
}