-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.htm
97 lines (97 loc) · 4.16 KB
/
errors.htm
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
<!DOCTYPE html>
<html lang="en">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="./site.css" rel="stylesheet">
<title>errors</title>
</head>
<body>
<div class="container">
<h2 id="pkg-overview">package errors</h2>
<p><code>import "errors"</code>
<p>errors包实现了创建错误值的函数。</p>
<div class="panel-group">
<div class="panel panel-default" id="example-package">
<div class="panel-heading" onclick="document.getElementById('ex-package').style.display = document.getElementById('ex-package').style.display=='none'?'block':'none';">Example</div>
<div id="ex-package" class="panel-collapse collapse">
<div class="panel-body">
<pre>package errors_test
import (
"fmt"
"time"
)
<span class="com">// MyError is an error implementation that includes a time and message.</span>
type MyError struct {
When time.Time
What string
}
func (e MyError) Error() string {
return fmt.Sprintf("%v: %v", e.When, e.What)
}
func oops() error {
return MyError{
time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
"the file system has gone away",
}
}
func Example() {
if err := oops(); err != nil {
fmt.Println(err)
}
<span class="com">// Output: 1989-03-15 22:30:00 +0000 UTC: the file system has gone away</span>
}
</pre>
</div>
</div>
</div>
</div>
<h3 id="pkg-index" class="section-header">Index <a class="permalink" href="#pkg-index">¶</a></h3>
<a href="../main.html"><h3>返回首页</h3></a>
</br>
<li><a href="#New">func New(text string) error</a></li>
</ul>
<h4 id="pkg-examples">Examples <a class="permalink" href="#pkg-index">¶</a></h4>
<a href="../main.html"><h3>返回首页</h3></a>
</br>
<li><a href="#example-New" onclick="$('#ex-New').addClass('in').removeClass('collapse').height('auto')">New</a></li>
<li><a href="#example-New--Errorf" onclick="$('#ex-New--Errorf').addClass('in').removeClass('collapse').height('auto')">New (Errorf)</a></li>
<li><a href="#example-package" onclick="$('#ex-package').addClass('in').removeClass('collapse').height('auto')">package</a></li>
</ul>
<h3 id="New">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/errors/errors.go?name=release#9">New</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func New(text <a href="builtin.htm#string">string</a>) <a href="builtin.htm#error">error</a></pre>
<p>使用字符串创建一个错误,请类比fmt包的Errorf方法,差不多可以认为是New(fmt.Sprintf(...))。</p>
<div class="panel-group">
<div class="panel panel-default" id="example-New">
<div class="panel-heading" onclick="document.getElementById('ex-New').style.display = document.getElementById('ex-New').style.display=='none'?'block':'none';">Example</div>
<div id="ex-New" class="panel-collapse collapse">
<div class="panel-body">
<pre>err := errors.New("emit macho dwarf: elf header corrupted")
if err != nil {
fmt.Print(err)
}</pre>
<p>Output:
<pre>emit macho dwarf: elf header corrupted
</pre>
</div>
</div>
</div>
<div class="panel panel-default" id="example-New--Errorf">
<div class="panel-heading" onclick="document.getElementById('ex-New--Errorf').style.display = document.getElementById('ex-New--Errorf').style.display=='none'?'block':'none';">Example (Errorf)</div>
<div id="ex-New--Errorf" class="panel-collapse collapse">
<div class="panel-body">
<pre>const name, id = "bimmler", 17
err := fmt.Errorf("user %q (id %d) not found", name, id)
if err != nil {
fmt.Print(err)
}</pre>
<p>Output:
<pre>user "bimmler" (id 17) not found
</pre>
</div>
</div>
</div>
</div>
</div>
</body>
</html>