mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:33:04 +08:00
校对13.4
This commit is contained in:
@@ -73,4 +73,4 @@ if err != nil {
|
|||||||
|
|
||||||
- [目录](directory.md)
|
- [目录](directory.md)
|
||||||
- 上一节:[错误处理](13.1.md)
|
- 上一节:[错误处理](13.1.md)
|
||||||
- 下一节:[从 panic 中恢复 (Recover)](13.3.md)
|
- 下一节:[从 panic 中恢复(Recover)](13.3.md)
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
# 13.3 从 panic 中恢复 (Recover)
|
# 13.3 从 panic 中恢复(Recover)
|
||||||
|
|
||||||
正如名字一样,这个(recover)内建函数被用于从 panic 或 错误场景中恢复:让程序可以从 panicking 重新获得控制权,停止终止过程进而恢复正常执行。
|
正如名字一样,这个(recover)内建函数被用于从 panic 或 错误场景中恢复:让程序可以从 panicking 重新获得控制权,停止终止过程进而恢复正常执行。
|
||||||
|
|
||||||
|
@@ -26,45 +26,45 @@ import (
|
|||||||
|
|
||||||
// A ParseError indicates an error in converting a word into an integer.
|
// A ParseError indicates an error in converting a word into an integer.
|
||||||
type ParseError struct {
|
type ParseError struct {
|
||||||
Index int // The index into the space-separated list of words.
|
Index int // The index into the space-separated list of words.
|
||||||
Word string // The word that generated the parse error.
|
Word string // The word that generated the parse error.
|
||||||
Err error // The raw error that precipitated this error, if any.
|
Err error // The raw error that precipitated this error, if any.
|
||||||
}
|
}
|
||||||
|
|
||||||
// String returns a human-readable error message.
|
// String returns a human-readable error message.
|
||||||
func (e *ParseError) String() string {
|
func (e *ParseError) String() string {
|
||||||
return fmt.Sprintf("pkg parse: error parsing %q as int", e.Word)
|
return fmt.Sprintf("pkg parse: error parsing %q as int", e.Word)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse parses the space-separated words in in put as integers.
|
// Parse parses the space-separated words in in put as integers.
|
||||||
func Parse(input string) (numbers []int, err error) {
|
func Parse(input string) (numbers []int, err error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
var ok bool
|
var ok bool
|
||||||
err, ok = r.(error)
|
err, ok = r.(error)
|
||||||
if !ok {
|
if !ok {
|
||||||
err = fmt.Errorf("pkg: %v", r)
|
err = fmt.Errorf("pkg: %v", r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
fields := strings.Fields(input)
|
fields := strings.Fields(input)
|
||||||
numbers = fields2numbers(fields)
|
numbers = fields2numbers(fields)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func fields2numbers(fields []string) (numbers []int) {
|
func fields2numbers(fields []string) (numbers []int) {
|
||||||
if len(fields) == 0 {
|
if len(fields) == 0 {
|
||||||
panic("no words to parse")
|
panic("no words to parse")
|
||||||
|
}
|
||||||
|
for idx, field := range fields {
|
||||||
|
num, err := strconv.Atoi(field)
|
||||||
|
if err != nil {
|
||||||
|
panic(&ParseError{idx, field, err})
|
||||||
}
|
}
|
||||||
for idx, field := range fields {
|
numbers = append(numbers, num)
|
||||||
num, err := strconv.Atoi(field)
|
}
|
||||||
if err != nil {
|
return
|
||||||
panic(&ParseError{idx, field, err})
|
|
||||||
}
|
|
||||||
numbers = append(numbers, num)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -81,23 +81,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var examples = []string{
|
var examples = []string{
|
||||||
"1 2 3 4 5",
|
"1 2 3 4 5",
|
||||||
"100 50 25 12.5 6.25",
|
"100 50 25 12.5 6.25",
|
||||||
"2 + 2 = 4",
|
"2 + 2 = 4",
|
||||||
"1st class",
|
"1st class",
|
||||||
"",
|
"",
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ex := range examples {
|
for _, ex := range examples {
|
||||||
fmt.Printf("Parsing %q:\n ", ex)
|
fmt.Printf("Parsing %q:\n ", ex)
|
||||||
nums, err := parse.Parse(ex)
|
nums, err := parse.Parse(ex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err) // here String() method from ParseError is used
|
fmt.Println(err) // here String() method from ParseError is used
|
||||||
continue
|
continue
|
||||||
}
|
|
||||||
fmt.Println(nums)
|
|
||||||
}
|
}
|
||||||
|
fmt.Println(nums)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -122,5 +122,5 @@ Parsing "":
|
|||||||
## 链接
|
## 链接
|
||||||
|
|
||||||
- [目录](directory.md)
|
- [目录](directory.md)
|
||||||
- 上一节:[从 panic 中恢复 (Recover)](13.3.md)
|
- 上一节:[从 panic 中恢复(Recover)](13.3.md)
|
||||||
- 下一节:[一种用闭包处理错误的模式](13.5.md)
|
- 下一节:[一种用闭包处理错误的模式](13.5.md)
|
||||||
|
@@ -127,7 +127,7 @@
|
|||||||
- 第13章:[错误处理与测试](13.0.md)
|
- 第13章:[错误处理与测试](13.0.md)
|
||||||
- 13.1 [错误处理](13.1.md)
|
- 13.1 [错误处理](13.1.md)
|
||||||
- 13.2 [运行时异常和 panic](13.2.md)
|
- 13.2 [运行时异常和 panic](13.2.md)
|
||||||
- 13.3 [从 panic 中恢复 (Recover)](13.3.md)
|
- 13.3 [从 panic 中恢复(Recover)](13.3.md)
|
||||||
- 13.4 [自定义包中的错误处理和 panicking](13.4.md)
|
- 13.4 [自定义包中的错误处理和 panicking](13.4.md)
|
||||||
- 13.5 [一种用闭包处理错误的模式](13.5.md)
|
- 13.5 [一种用闭包处理错误的模式](13.5.md)
|
||||||
- 13.6 [启动外部命令和程序](13.6.md)
|
- 13.6 [启动外部命令和程序](13.6.md)
|
||||||
|
Reference in New Issue
Block a user