更新13.9 (#280)

* Update 07.3.md

* Update 07.3.md

* Update 07.6.md

* fix few problems

* Update 10.6 and 10.8

* Update 11.6 and 11.7

* fix some problems before Chap13

* add a dot

* Update 13.9
This commit is contained in:
王耀
2016-09-02 10:14:40 +08:00
committed by 无闻
parent b0715416ec
commit f27a8d84bf
3 changed files with 11 additions and 11 deletions

View File

@@ -44,7 +44,7 @@ func main() {
```go ```go
func Sqrt(f float64) (float64, error) { func Sqrt(f float64) (float64, error) {
if f < 0 { if f < 0 {
return 0, errors.New (math - square root of negative number) return 0, errors.New ("math - square root of negative number")
} }
// implementation of Sqrt // implementation of Sqrt
} }
@@ -54,7 +54,7 @@ func Sqrt(f float64) (float64, error) {
```go ```go
if f, err := Sqrt(-1); err != nil { if f, err := Sqrt(-1); err != nil {
fmt.Printf(Error: %s\n, err) fmt.Printf("Error: %s\n", err)
} }
``` ```
@@ -65,13 +65,13 @@ if f, err := Sqrt(-1); err != nil {
```go ```go
// PathError records an error and the operation and file path that caused it. // PathError records an error and the operation and file path that caused it.
type PathError struct { type PathError struct {
Op string // open, unlink, etc. Op string // "open", "unlink", etc.
Path string // The associated file. Path string // The associated file.
Err error // Returned by the system call. Err error // Returned by the system call.
} }
func (e *PathError) String() string { func (e *PathError) String() string {
return e.Op + + e.Path + : + e.Err.Error() return e.Op + " " + e.Path + ": "+ e.Err.Error()
} }
``` ```
@@ -94,7 +94,7 @@ switch err := err.(type) {
PrintPathError(err) PrintPathError(err)
... ...
default: default:
fmt.Printf(Not a special error, just %s\n, err) fmt.Printf("Not a special error, just %s\n", err)
} }
``` ```
@@ -115,7 +115,7 @@ func (e *SyntaxError) String() string { return e.msg }
```go ```go
if serr, ok := err.(*json.SyntaxError); ok { if serr, ok := err.(*json.SyntaxError); ok {
line, col := findLine(f, serr.Offset) line, col := findLine(f, serr.Offset)
return fmt.Errorf(%s:%d:%d: %v, f.Name(), line, col, err) return fmt.Errorf("%s:%d:%d: %v", f.Name(), line, col, err)
} }
``` ```
@@ -165,15 +165,15 @@ var (
```go ```go
if f < 0 { if f < 0 {
return 0, fmt.Errorf(math: square root of negative number %g, f) return 0, fmt.Errorf("math: square root of negative number %g", f)
} }
``` ```
第二个例子:从命令行读取输入时,如果加了 help 标志,我们可以用有用的信息产生一个错误: 第二个例子:从命令行读取输入时,如果加了 help 标志,我们可以用有用的信息产生一个错误:
```go ```go
if len(os.Args) > 1 && (os.Args[1] == -h || os.Args[1] == --help) { if len(os.Args) > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
err = fmt.Errorf(usage: %s infile.txt outfile.txt, filepath.Base(os.Args[0])) err = fmt.Errorf("usage: %s infile.txt outfile.txt", filepath.Base(os.Args[0]))
return return
} }
``` ```

View File

@@ -10,7 +10,7 @@
这在下面的代码中被很好地阐述了。我们有一个简单的 parse 包(示例 13.4)用来把输入的字符串解析为整数切片;这个包有自己特殊的 `ParseError` 这在下面的代码中被很好地阐述了。我们有一个简单的 parse 包(示例 13.4)用来把输入的字符串解析为整数切片;这个包有自己特殊的 `ParseError`
当没有东西需要转换或者转换成整数失败时,这个包会 panic在函数 fields2numbers 中)。但是可导出的 Parse 函数会从 panic 中 recover 并用所有这些信息返回一个错误给调用者。为了演示这个过程,在 panic_recover.go 中 调用了 parse 包(示例 13.4);不可解析的字符串会导致错误并被打印出来。 当没有东西需要转换或者转换成整数失败时,这个包会 panic在函数 fields2numbers 中)。但是可导出的 Parse 函数会从 panic 中 recover 并用所有这些信息返回一个错误给调用者。为了演示这个过程,在 [panic_recover.go](examples/chapter_13/parse/panic_recover.go) 中 调用了 parse 包(示例 13.4);不可解析的字符串会导致错误并被打印出来。
示例 13.4 [parse.go](examples/chapter_13/parse/parse.go) 示例 13.4 [parse.go](examples/chapter_13/parse/parse.go)

View File

@@ -32,7 +32,7 @@ func TestFunction(t *testing.T) {
```go ```go
func verify(t *testing.T, testnum int, testcase, input, output, expected string) { func verify(t *testing.T, testnum int, testcase, input, output, expected string) {
if input != output { if expected != output {
t.Errorf(%d. %s with input = %s: output %s != %s, testnum, testcase, input, output, expected) t.Errorf(%d. %s with input = %s: output %s != %s, testnum, testcase, input, output, expected)
} }
} }