From 036556386fdf9f5387f3a799fa851b019cb485e1 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 21 Apr 2014 05:24:31 -0400 Subject: [PATCH] 5.2 --- README.md | 2 +- eBook/05.2.md | 74 +++++++++++++++++++++++++++++++++++++-------------- eBook/05.3.md | 18 +++++++++++++ 3 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 eBook/05.3.md diff --git a/README.md b/README.md index 357c779ce..a8eea8688 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ ## 翻译进度 -5.1 [if-else 结构](eBook/05.1.md) +5.2 [测试多返回值函数的错误](eBook/05.2.md) ## 支持本书 diff --git a/eBook/05.2.md b/eBook/05.2.md index 39ab42a77..2cb768190 100644 --- a/eBook/05.2.md +++ b/eBook/05.2.md @@ -1,20 +1,3 @@ -## 啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0 - -要不等到 **2014 年 4 月 10 日** 再来看看吧~~ - -这里还有一些其它的学习资源噢~ - - - [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming) - - [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang) - - [《Go名库讲解》](https://github.com/Unknwon/go-rock-libraries-showcases) - -神马?你说你不想学习?那好吧,去逛逛看看行情也行~ - -- [Go Walker](https://gowalker.org) **Go 项目 API 文档在线浏览工具** -- [Go 中国社区](http://bbs.go-china.org) -- [Go语言学习园地](http://studygolang.com/) -- [Golang中国](http://golang.tc) - # 5.2 测试多返回值函数的错误 Go 语言的函数经常使用两个返回值来表示执行是否成功:返回某个值以及 true 表示成功;返回零值(或 nil)和 false 表示失败(第 4.4 节)。当不使用 true 或 false 的时候,也可以使用一个 error 类型的变量来代替作为第二个返回值:成功执行的话,error 的值为 nil,否则就会包含相应的错误信息(Go 语言中的错误类型为 error: `var err error`,我们将会在第 13 章进行更多地讨论)。这样一来,就很明显需要用一个 if 语句来测试执行结果;由于其符号的原因,这样的形式又称之为 comma,ok 模式(pattern)。 @@ -61,7 +44,7 @@ Example 5.3 [string_conversion2.go](examples/chapter_5/string_conversion2.go) 这是测试 err 变量是否包含一个真正的错误(`if err != nil`)的习惯用法。如果确实存在错误,则会打印相应的错误信息然后通过 return 提前结束函数的执行。我们还可以使用携带返回值的 return 形式,例如 `return err`。这样一来,函数的调用者就可以检查函数执行过程中是否存在错误了。 - **习惯用法** +**习惯用法** value, err := pack1.Function1(param1) if err!=nil { @@ -74,7 +57,7 @@ Example 5.3 [string_conversion2.go](examples/chapter_5/string_conversion2.go) 如果我们想要在错误发生的同时终止程序的运行,我们可以使用 `os` 包的 `Exit` 函数: - **习惯用法** +**习惯用法** if err !=nil { fmt.Printf("Program stopping with error %v", err) @@ -98,7 +81,58 @@ Example 5.3 [string_conversion2.go](examples/chapter_5/string_conversion2.go) **练习 5.1** 尝试改写 [string_conversion2.go](examples/chapter_5/string_conversion2.go) 中的代码,要求使用 `:=` 方法来对 err 进行赋值,哪些地方可以被修改? -示例 3: +示例 3:可以将错误的获取放置在 if 语句的初始化部分: + +**习惯用法** + + if err := file.Chmod(0664); err !=nil { + fmt.Println(err) + return err + } + +示例 4:或者将 ok-pattern 的获取放置在 if 语句的初始化部分,然后进行判断: + +**习惯用法** + + if value, ok := readData(); ok { + … + } + +**注意事项** + +如果您想下面一样,没有为多返回值的函数准备足够的变量来存放结果: + + func mySqrt(f float64) (v float64, ok bool) { + if f < 0 { return } // error case + return math.Sqrt(f),true + } + + func main() { + t := mySqrt(25.0) + fmt.Println(t) + } + +您会得到一个编译错误:`multiple-value mySqrt() in single-value context`。 + +正确的做法是: + + t, ok := mySqrt(25.0) + if ok { fmt.Println(t) } + +**注意事项 2** + +当您将字符串转换为整数时,且确定转换一定能够成功时,可以将 `Atoi` 函数进行一层忽略错误的封装: + + func atoi (s string) (n int) { + n, _ = strconv.Atoi(s) + return + } + +实际上,`fmt` 包(第 4.4.3 节)最简单的打印函数也有 2 个返回值: + + count, err := fmt.Println(x) // number of bytes printed, nil or 0, error + +当打印到控制台时,可以将该函数返回的错误忽略;但当输出到文件流、网络流等具有不确定因素的输出对象时,应该始终检查是否有错误发生(另见 练习 6.1b)。 ## 链接 diff --git a/eBook/05.3.md b/eBook/05.3.md new file mode 100644 index 000000000..dc986872e --- /dev/null +++ b/eBook/05.3.md @@ -0,0 +1,18 @@ +## 啊哦,亲,你看得也太快了。。。还没翻译完呢 0 0 + +要不等到 **2014 年 4 月 22 日** 再来看看吧~~ + +这里还有一些其它的学习资源噢~ + + - [《Go编程基础》](https://github.com/Unknwon/go-fundamental-programming) + - [《Go Web编程》](https://github.com/astaxie/build-web-application-with-golang) + - [《Go名库讲解》](https://github.com/Unknwon/go-rock-libraries-showcases) + +神马?你说你不想学习?那好吧,去逛逛看看行情也行~ + +- [Go Walker](https://gowalker.org) **Go 项目 API 文档在线浏览工具** +- [Go 中国社区](http://bbs.go-china.org) +- [Go语言学习园地](http://studygolang.com/) +- [Golang中国](http://golangtc.com) + +# 5.3 switch 结构 \ No newline at end of file