forked from gobyexample-cn/gobyexample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
159 lines (115 loc) · 5.14 KB
/
index.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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8">
<title>Go by Example 中文:Panic</title>
<link rel=stylesheet href="../site.css">
<link rel="canonical" href="http://gobyexample.everyx.in/panic/" />
</head>
<body>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-53092201-2', 'auto');
ga('send', 'pageview');
</script>
<div class="example" id="panic">
<h2><a href="../">Go by Example 中文</a>:Panic</h2>
<table>
<tr>
<td class="docs">
<p><code>panic</code> 意味着有些出乎意料的错误发生。通常我们用它来表示程序正常运行中不应该出现的,后者我么没有处理好的错误。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="http://play.golang.org/p/nWYPMt6EBR"><img title="Run code" src="../play.png" class="run" /></a>
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kn">import</span> <span class="s">"os"</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>我们将在真个网站中使用 panic 来检查预期外的错误。这个是唯一一个为 panic 准备的例子。</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nb">panic</span><span class="p">(</span><span class="s">"a problem"</span><span class="p">)</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>panic 的一个基本用法就是在一个函数返回了错误值但是我们并不知道(或者不想)处理时终止运行。这里是一个在创建一个新文件时返回异常错误时的<code>panic</code> 用法。</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">_</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">os</span><span class="p">.</span><span class="nx">Create</span><span class="p">(</span><span class="s">"/tmp/file"</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">err</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nb">panic</span><span class="p">(</span><span class="nx">err</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>运行程序将会引起 panic,输出一个错误消息和 Go 运行时栈信息,并且返回一个非零的状态码。</p>
</td>
<td class="code leading">
<div class="highlight"><pre><span class="gp">$</span> go run panic.go
<span class="go">panic: a problem</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<div class="highlight"><pre><span class="go">goroutine 1 [running]:</span>
<span class="go">main.main()</span>
<span class="go"> /.../panic.go:12 +0x47</span>
<span class="go">...</span>
<span class="go">exit status 2</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>注意,不像有些语言中处理多个错误那样,在 Go 中习惯使用错误码返回任意可能的值。</p>
</td>
<td class="code empty">
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="../defer/">Defer</a>.
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 编写 | <a href="https://plus.google.com/u/0/113729799374513477039">everyx</a> 翻译 | <a href="https://github.com/everyx/gobyexample">项目地址</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a>
</p>
</div>
</body>
</html>