Skip to content

Commit a93ea1b

Browse files
committed
第一小节完成了一部分
1 parent f6b4221 commit a93ea1b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

11.1.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,29 @@
11
# 11.1 错误处理
2+
Go语言设计的时候主要的特点是:简洁、明白,简洁是指语法和C类似,相当的简单,明白是指任何语句都是很明显的,不含有任何隐式的东西,Go在设计错误的时候也是一样。我们知道C语言里面返回错误是使用-1或者nil之类的返回信息表示错误,但是对于使用者来说这个返回值根本不知道什么意思,而Go里面当发生异常时返回一个Error类型,通过前面编写的代码,我们发现在Go语言里面有很多地方都使用了Error类型,很多函数都有这个Error返回。例如`os.Open`函数当打开文件失败时返回一个不为nil的error
3+
4+
func Open(name string) (file *File, err error)
5+
6+
下面这个例子通过`os.Open`打开一个文件,如果出错那么会执行`log.Fatal`打印出来错误信息:
7+
8+
f, err := os.Open("filename.ext")
9+
if err != nil {
10+
log.Fatal(err)
11+
}
12+
13+
其实这样的error返回在Go语言的很多内置包里面有很多,我们这个小节将详细的介绍这些error是怎么设计的,以及在我们设计的Web应用如何更好的处理error。
14+
## Error类型
15+
error类型是一个接口类型,这是他的定义:
16+
17+
type error interface {
18+
Error() string
19+
}
20+
21+
22+
## 自定义Error
23+
24+
## Error检测
25+
26+
## 总结
227

328
## links
429
* [目录](<preface.md>)

0 commit comments

Comments
 (0)