mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-11-13 09:16:10 +08:00
修改部分描述,添加必要的标点符号,补充代码超链接 (#804)
This commit is contained in:
@@ -28,7 +28,7 @@ fType1 = func f(a type1, b type2)
|
||||
func check(err error) { if err != nil { panic(err) } }
|
||||
```
|
||||
|
||||
2)errorhandler:这是一个包装函数。接收一个 fType1 类型的函数 fn 并返回一个调用 fn 的函数。里面就包含有 defer/recover 机制,这在 [13.3 节](13.3.md)中有相应描述。
|
||||
2)errorhandler:这是一个包装函数。接收一个 fType1 类型的函数 fn 并返回一个调用 fn 的函数。里面就包含有 defer/recover 机制,这在 [13.3 节](13.3.md) 中有相应描述。
|
||||
|
||||
```go
|
||||
func errorHandler(fn fType1) fType1 {
|
||||
@@ -60,9 +60,7 @@ func f1(a type1, b type2) {
|
||||
|
||||
通过这种机制,所有的错误都会被 recover,并且调用函数后的错误检查代码也被简化为调用 check(err) 即可。在这种模式下,不同的错误处理必须对应不同的函数类型;它们(错误处理)可能被隐藏在错误处理包内部。可选的更加通用的方式是用一个空接口类型的切片作为参数和返回值。
|
||||
|
||||
我们会在 [15.5 节](15.5.md)的 web 应用中使用这种模式。
|
||||
|
||||
<u>练习</u>
|
||||
我们会在 [15.5 节](15.5.md) 的 web 应用中使用这种模式。
|
||||
|
||||
**练习 13.1**:[recover_dividebyzero.go](exercises/chapter_13/recover_divbyzero.go)
|
||||
|
||||
@@ -124,7 +122,7 @@ Returned normally from f.
|
||||
|
||||
**练习 13.3**:[panic_defer_convint.go](exercises/chapter_13/panic_defer_convint.go)
|
||||
|
||||
写一个 ConvertInt64ToInt 函数把 int64 值转换为 int 值,如果发生错误(提示:参见 [4.5.2.1 节](04.5.md#4521-整型-int-和浮点型-float))就 panic。然后在函数 IntFromInt64 中调用这个函数并 recover,返回一个整数和一个错误。请测试这个函数!
|
||||
写一个 ConvertInt64ToInt 函数把 int64 值转换为 int 值,如果发生错误(提示:参见 [4.5.2.1 节](04.5.md#4521-整型-int-和浮点型-float))就 panic 。然后在函数 IntFromInt64 中调用这个函数并 recover,返回一个整数和一个错误。请测试这个函数!
|
||||
|
||||
## 链接
|
||||
|
||||
|
||||
Reference in New Issue
Block a user