在go语言编程中,它习惯于通过一个显式的,单独的返回值来传达错误。这与java和ruby等语言中使用的异常,以及有时在c语言中使用的重载的单个结果/错误值形成对比。go语言编程的方法使得很容易看到哪些函数返回错误,并使用用于任何其他语言的相同语言构造来处理它们,非错误任务。
按照惯例,错误是最后一个返回值,并有类型:error
,它是一个内置接口。通过对它们实现error()
方法,可以使用自定义类型作为错误。上面的例子使用自定义类型来显式地表示一个参数错误。
errors.new
使用给定的错误消息构造基本错误值。错误位置中的nil
值表示没有错误。
在这种情况下,使用&argerror
语法构建一个新的结构,为两个字段arg
和prob
提供值。
下面的两个循环测试每个错误返回函数。注意,使用if
语句内联错误检查是go
代码中的常见作法。
如果要以编程方式使用自定义错误中的数据,则需要通过类型断言将错误作为自定义错误类型的实例。
所有的示例代码,都放在
f:\worksp\golang
目录下。安装go编程环境请参考:http://www.h3.com/go/go_environment.html
errors.go
的完整代码如下所示 -
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)
}
}
执行上面代码,将得到以下输出结果 -
f:\worksp\golang>go run errors.go
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