errors _ golang
jackkiexu · · 2583 次点击 · · 开始浏览In Go it's idiomatic to communicate errors via an explicit, separate return value. this constrasts errors via an explicit, separate return value. This constrasts with the exceptions used in languages like Java and Ruby and the overloaded single result / error value sometimes used in C. Go's approach makes it easy to see which functions return errors and to handle them using the same language constructs employed for any other, non-error tasks
package main import ( "errors" "fmt" ) func f1(arg int) (int, error) { if arg == 42 { return -1, errors.New("can't work with 42") } return arg + 3, nil } 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 { return -1, &argError{arg, "can't work with it"} } return arg + 3, nil } func main() { for _, i := range []int{7, 42} { if r, e := f1(i); e != nil { fmt.Println("f1 failed : ", e) } else { fmt.Println("f2 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) } } _, e := f2(42) if ae, ok := e.(*argError); ok { fmt.Println(ae.arg) fmt.Println(ae.prob) } }
f2 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
总结 :
1 : golang 的 内建函数 builtin.go 里面定义了 type error interface{ Error() string }; 实现了这个方法就是 自定义的 Error(查看源码 command + shift + 鼠标左键)
2 : ....
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
In Go it's idiomatic to communicate errors via an explicit, separate return value. this constrasts errors via an explicit, separate return value. This constrasts with the exceptions used in languages like Java and Ruby and the overloaded single result / error value sometimes used in C. Go's approach makes it easy to see which functions return errors and to handle them using the same language constructs employed for any other, non-error tasks
package main import ( "errors" "fmt" ) func f1(arg int) (int, error) { if arg == 42 { return -1, errors.New("can't work with 42") } return arg + 3, nil } 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 { return -1, &argError{arg, "can't work with it"} } return arg + 3, nil } func main() { for _, i := range []int{7, 42} { if r, e := f1(i); e != nil { fmt.Println("f1 failed : ", e) } else { fmt.Println("f2 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) } } _, e := f2(42) if ae, ok := e.(*argError); ok { fmt.Println(ae.arg) fmt.Println(ae.prob) } }
f2 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
总结 :
1 : golang 的 内建函数 builtin.go 里面定义了 type error interface{ Error() string }; 实现了这个方法就是 自定义的 Error(查看源码 command + shift + 鼠标左键)
2 : ....