You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

329 lines
8.6 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>errors - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package errors</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "errors"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package errors implements functions to manipulate errors.
  69. </p>
  70. </div>
  71. </div>
  72. <div id="example_" class="toggle">
  73. <div class="collapsed">
  74. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  75. </div>
  76. <div class="expanded">
  77. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  78. <p>Code:</p>
  79. <pre class="code">package errors_test
  80. import (
  81. &#34;fmt&#34;
  82. &#34;time&#34;
  83. )
  84. <span class="comment">// MyError is an error implementation that includes a time and message.</span>
  85. type MyError struct {
  86. When time.Time
  87. What string
  88. }
  89. func (e MyError) Error() string {
  90. return fmt.Sprintf(&#34;%v: %v&#34;, e.When, e.What)
  91. }
  92. func oops() error {
  93. return MyError{
  94. time.Date(1989, 3, 15, 22, 30, 0, 0, time.UTC),
  95. &#34;the file system has gone away&#34;,
  96. }
  97. }
  98. func Example() {
  99. if err := oops(); err != nil {
  100. fmt.Println(err)
  101. }
  102. <span class="comment">// Output: 1989-03-15 22:30:00 +0000 UTC: the file system has gone away</span>
  103. }
  104. </pre>
  105. </div>
  106. </div>
  107. <div id="pkg-index" class="toggleVisible">
  108. <div class="collapsed">
  109. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  110. </div>
  111. <div class="expanded">
  112. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  113. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  114. <div id="manual-nav">
  115. <dl>
  116. <dd><a href="index.html#New">func New(text string) error</a></dd>
  117. </dl>
  118. </div><!-- #manual-nav -->
  119. <div id="pkg-examples">
  120. <h4>Examples</h4>
  121. <dl>
  122. <dd><a class="exampleLink" href="index.html#example_">Package</a></dd>
  123. <dd><a class="exampleLink" href="index.html#example_New">New</a></dd>
  124. <dd><a class="exampleLink" href="index.html#example_New_errorf">New (Errorf)</a></dd>
  125. </dl>
  126. </div>
  127. <h4>Package files</h4>
  128. <p>
  129. <span style="font-size:90%">
  130. <a href="http://localhost:6060/src/errors/errors.go">errors.go</a>
  131. </span>
  132. </p>
  133. </div><!-- .expanded -->
  134. </div><!-- #pkg-index -->
  135. <div id="pkg-callgraph" class="toggle" style="display: none">
  136. <div class="collapsed">
  137. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  138. </div> <!-- .expanded -->
  139. <div class="expanded">
  140. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  141. <p>
  142. In the call graph viewer below, each node
  143. is a function belonging to this package
  144. and its children are the functions it
  145. calls&mdash;perhaps dynamically.
  146. </p>
  147. <p>
  148. The root nodes are the entry points of the
  149. package: functions that may be called from
  150. outside the package.
  151. There may be non-exported or anonymous
  152. functions among them if they are called
  153. dynamically from another package.
  154. </p>
  155. <p>
  156. Click a node to visit that function's source code.
  157. From there you can visit its callers by
  158. clicking its declaring <code>func</code>
  159. token.
  160. </p>
  161. <p>
  162. Functions may be omitted if they were
  163. determined to be unreachable in the
  164. particular programs or tests that were
  165. analyzed.
  166. </p>
  167. <!-- Zero means show all package entry points. -->
  168. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  169. </div>
  170. </div> <!-- #pkg-callgraph -->
  171. <h2 id="New">func <a href="http://localhost:6060/src/errors/errors.go?s=294:321#L1">New</a></h2>
  172. <pre>func New(text <a href="../builtin/index.html#string">string</a>) <a href="../builtin/index.html#error">error</a></pre>
  173. <p>
  174. New returns an error that formats as the given text.
  175. </p>
  176. <div id="example_New" class="toggle">
  177. <div class="collapsed">
  178. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  179. </div>
  180. <div class="expanded">
  181. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  182. <p>Code:</p>
  183. <pre class="code">err := errors.New(&#34;emit macho dwarf: elf header corrupted&#34;)
  184. if err != nil {
  185. fmt.Print(err)
  186. }
  187. <span class="comment"></pre>
  188. <p>Output:</p>
  189. <pre class="output">emit macho dwarf: elf header corrupted
  190. </pre>
  191. </div>
  192. </div>
  193. <div id="example_New_errorf" class="toggle">
  194. <div class="collapsed">
  195. <p class="exampleHeading toggleButton"><span class="text">Example (Errorf)</span></p>
  196. </div>
  197. <div class="expanded">
  198. <p class="exampleHeading toggleButton"><span class="text">Example (Errorf)</span></p>
  199. <p>The fmt package&#39;s Errorf function lets us use the package&#39;s formatting
  200. features to create descriptive error messages.
  201. </p>
  202. <p>Code:</p>
  203. <pre class="code">const name, id = &#34;bimmler&#34;, 17
  204. err := fmt.Errorf(&#34;user %q (id %d) not found&#34;, name, id)
  205. if err != nil {
  206. fmt.Print(err)
  207. }
  208. <span class="comment"></pre>
  209. <p>Output:</p>
  210. <pre class="output">user &#34;bimmler&#34; (id 17) not found
  211. </pre>
  212. </div>
  213. </div>
  214. <div id="footer">
  215. Build version go1.6.<br>
  216. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  217. the content of this page is licensed under the
  218. Creative Commons Attribution 3.0 License,
  219. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  220. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  221. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  222. </div>
  223. </div><!-- .container -->
  224. </div><!-- #page -->
  225. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  226. <script type="text/javascript" src="../../lib/godoc/jquery.js"></script>
  227. <script type="text/javascript" src="../../lib/godoc/jquery.treeview.js"></script>
  228. <script type="text/javascript" src="../../lib/godoc/jquery.treeview.edit.js"></script>
  229. <script type="text/javascript" src="../../lib/godoc/godocs.js"></script>
  230. </body>
  231. </html>