mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 00:43:26 +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
|
||||
}
|
||||
```
|
||||
|
Reference in New Issue
Block a user