-
Notifications
You must be signed in to change notification settings - Fork 8
/
errors.html
287 lines (224 loc) · 14.7 KB
/
errors.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 错误处理</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'generics';
}
if (e.key == "ArrowRight") {
window.location.href = 'goroutines';
}
}
</script>
<body>
<div class="example" id="errors">
<h2><a href="./">Go by Example 中文版</a>: 错误处理</h2>
<table>
<tr>
<td class="docs">
<p>符合 Go 语言习惯的做法是使用一个独立、明确的返回值来传递错误信息。
这与 Java、Ruby 使用的异常(exception)
以及在 C 语言中有时用到的重载 (overloaded) 的单返回/错误值有着明显的不同。
Go 语言的处理方式能清楚的知道哪个函数返回了错误,并使用跟其他(无异常处理的)语言类似的方式来处理错误。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/jbP3g3b2gCY"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"errors"</span>
<span class="s">"fmt"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>按照惯例,错误通常是最后一个返回值并且是 <code>error</code> 类型,它是一个内建的接口。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">f1</span><span class="p">(</span><span class="nx">arg</span> <span class="kt">int</span><span class="p">)</span> <span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">error</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="nx">arg</span> <span class="o">==</span> <span class="mi">42</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>errors.New</code> 使用给定的错误信息构造一个基本的 <code>error</code> 值。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="nx">errors</span><span class="p">.</span><span class="nf">New</span><span class="p">(</span><span class="s">"can't work with 42"</span><span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>返回错误值为 nil 代表没有错误。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">return</span> <span class="nx">arg</span> <span class="o">+</span> <span class="mi">3</span><span class="p">,</span> <span class="kc">nil</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>你还可以通过实现 <code>Error()</code> 方法来自定义 <code>error</code> 类型。
这里使用自定义错误类型来表示上面例子中的参数错误。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">type</span> <span class="nx">argError</span> <span class="kd">struct</span> <span class="p">{</span>
<span class="nx">arg</span> <span class="kt">int</span>
<span class="nx">prob</span> <span class="kt">string</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="p">(</span><span class="nx">e</span> <span class="o">*</span><span class="nx">argError</span><span class="p">)</span> <span class="nf">Error</span><span class="p">()</span> <span class="kt">string</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Sprintf</span><span class="p">(</span><span class="s">"%d - %s"</span><span class="p">,</span> <span class="nx">e</span><span class="p">.</span><span class="nx">arg</span><span class="p">,</span> <span class="nx">e</span><span class="p">.</span><span class="nx">prob</span><span class="p">)</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">f2</span><span class="p">(</span><span class="nx">arg</span> <span class="kt">int</span><span class="p">)</span> <span class="p">(</span><span class="kt">int</span><span class="p">,</span> <span class="kt">error</span><span class="p">)</span> <span class="p">{</span>
<span class="k">if</span> <span class="nx">arg</span> <span class="o">==</span> <span class="mi">42</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>在这个例子中,我们使用 <code>&argError</code> 语法来建立一个新的结构体,
并提供了 <code>arg</code> 和 <code>prob</code> 两个字段的值。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">return</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">&</span><span class="nx">argError</span><span class="p">{</span><span class="nx">arg</span><span class="p">,</span> <span class="s">"can't work with it"</span><span class="p">}</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">arg</span> <span class="o">+</span> <span class="mi">3</span><span class="p">,</span> <span class="kc">nil</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kd">func</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>下面的两个循环测试了每一个会返回错误的函数。
注意,在 <code>if</code> 的同一行进行错误检查,是 Go 代码中的一种常见用法。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">i</span> <span class="o">:=</span> <span class="k">range</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="mi">7</span><span class="p">,</span> <span class="mi">42</span><span class="p">}</span> <span class="p">{</span>
<span class="k">if</span> <span class="nx">r</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="nf">f1</span><span class="p">(</span><span class="nx">i</span><span class="p">);</span> <span class="nx">e</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"f1 failed:"</span><span class="p">,</span> <span class="nx">e</span><span class="p">)</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"f1 worked:"</span><span class="p">,</span> <span class="nx">r</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">i</span> <span class="o">:=</span> <span class="k">range</span> <span class="p">[]</span><span class="kt">int</span><span class="p">{</span><span class="mi">7</span><span class="p">,</span> <span class="mi">42</span><span class="p">}</span> <span class="p">{</span>
<span class="k">if</span> <span class="nx">r</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="nf">f2</span><span class="p">(</span><span class="nx">i</span><span class="p">);</span> <span class="nx">e</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"f2 failed:"</span><span class="p">,</span> <span class="nx">e</span><span class="p">)</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="s">"f2 worked:"</span><span class="p">,</span> <span class="nx">r</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>如果你想在程序中使用自定义错误类型的数据,
你需要通过类型断言来得到这个自定义错误类型的实例。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="nx">_</span><span class="p">,</span> <span class="nx">e</span> <span class="o">:=</span> <span class="nf">f2</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">ae</span><span class="p">,</span> <span class="nx">ok</span> <span class="o">:=</span> <span class="nx">e</span><span class="p">.(</span><span class="o">*</span><span class="nx">argError</span><span class="p">);</span> <span class="nx">ok</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">ae</span><span class="p">.</span><span class="nx">arg</span><span class="p">)</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nf">Println</span><span class="p">(</span><span class="nx">ae</span><span class="p">.</span><span class="nx">prob</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="gp">$</span> go run errors.go
<span class="go">f1 worked: 10
</span><span class="go">f1 failed: can't work with 42
</span><span class="go">f2 worked: 10
</span><span class="go">f2 failed: 42 - can't work with it
</span><span class="go">42
</span><span class="go">can't work with it</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>到 Go 官方博客去看这篇<a href="http://blog.golang.org/2011/07/error-handling-and-go.html">很棒的文章</a>,
以获取更多关于错误处理的信息。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="goroutines">协程</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"errors\"\u000A \"fmt\"\u000A)\u000A');codeLines.push('func f1(arg int) (int, error) {\u000A if arg \u003D\u003D 42 {\u000A');codeLines.push(' return -1, errors.New(\"can\'t work with 42\")\u000A');codeLines.push(' }\u000A');codeLines.push(' return arg + 3, nil\u000A}\u000A');codeLines.push('type argError struct {\u000A arg int\u000A prob string\u000A}\u000A');codeLines.push('func (e *argError) Error() string {\u000A return fmt.Sprintf(\"%d - %s\", e.arg, e.prob)\u000A}\u000A');codeLines.push('func f2(arg int) (int, error) {\u000A if arg \u003D\u003D 42 {\u000A');codeLines.push(' return -1, \u0026argError{arg, \"can\'t work with it\"}\u000A }\u000A return arg + 3, nil\u000A}\u000A');codeLines.push('func main() {\u000A');codeLines.push(' for _, i :\u003D range []int{7, 42} {\u000A if r, e :\u003D f1(i); e !\u003D nil {\u000A fmt.Println(\"f1 failed:\", e)\u000A } else {\u000A fmt.Println(\"f1 worked:\", r)\u000A }\u000A }\u000A for _, i :\u003D range []int{7, 42} {\u000A if r, e :\u003D f2(i); e !\u003D nil {\u000A fmt.Println(\"f2 failed:\", e)\u000A } else {\u000A fmt.Println(\"f2 worked:\", r)\u000A }\u000A }\u000A');codeLines.push(' _, e :\u003D f2(42)\u000A if ae, ok :\u003D e.(*argError); ok {\u000A fmt.Println(ae.arg)\u000A fmt.Println(ae.prob)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>