mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-11 23:52:31 +08:00
更新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:
@@ -44,7 +44,7 @@ func main() {
|
||||
```go
|
||||
func Sqrt(f float64) (float64, error) {
|
||||
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
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func Sqrt(f float64) (float64, error) {
|
||||
|
||||
```go
|
||||
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
|
||||
// PathError records an error and the operation and file path that caused it.
|
||||
type PathError struct {
|
||||
Op string // “open”, “unlink”, etc.
|
||||
Op string // "open", "unlink", etc.
|
||||
Path string // The associated file.
|
||||
Err error // Returned by the system call.
|
||||
}
|
||||
|
||||
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)
|
||||
...
|
||||
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
|
||||
if serr, ok := err.(*json.SyntaxError); ok {
|
||||
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
|
||||
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 标志,我们可以用有用的信息产生一个错误:
|
||||
|
||||
```go
|
||||
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]))
|
||||
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]))
|
||||
return
|
||||
}
|
||||
```
|
||||
|
@@ -10,7 +10,7 @@
|
||||
|
||||
这在下面的代码中被很好地阐述了。我们有一个简单的 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):
|
||||
|
||||
|
@@ -32,7 +32,7 @@ func TestFunction(t *testing.T) {
|
||||
|
||||
```go
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user