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.

475 lines
14 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>ioutil - 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 ioutil</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 "io/ioutil"</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 ioutil implements some I/O utility functions.
  69. </p>
  70. </div>
  71. </div>
  72. <div id="pkg-index" class="toggleVisible">
  73. <div class="collapsed">
  74. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  75. </div>
  76. <div class="expanded">
  77. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  78. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  79. <div id="manual-nav">
  80. <dl>
  81. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  82. <dd><a href="index.html#NopCloser">func NopCloser(r io.Reader) io.ReadCloser</a></dd>
  83. <dd><a href="index.html#ReadAll">func ReadAll(r io.Reader) ([]byte, error)</a></dd>
  84. <dd><a href="index.html#ReadDir">func ReadDir(dirname string) ([]os.FileInfo, error)</a></dd>
  85. <dd><a href="index.html#ReadFile">func ReadFile(filename string) ([]byte, error)</a></dd>
  86. <dd><a href="index.html#TempDir">func TempDir(dir, prefix string) (name string, err error)</a></dd>
  87. <dd><a href="index.html#TempFile">func TempFile(dir, prefix string) (f *os.File, err error)</a></dd>
  88. <dd><a href="index.html#WriteFile">func WriteFile(filename string, data []byte, perm os.FileMode) error</a></dd>
  89. </dl>
  90. </div><!-- #manual-nav -->
  91. <div id="pkg-examples">
  92. <h4>Examples</h4>
  93. <dl>
  94. <dd><a class="exampleLink" href="index.html#example_ReadAll">ReadAll</a></dd>
  95. <dd><a class="exampleLink" href="index.html#example_ReadDir">ReadDir</a></dd>
  96. <dd><a class="exampleLink" href="index.html#example_TempDir">TempDir</a></dd>
  97. <dd><a class="exampleLink" href="index.html#example_TempFile">TempFile</a></dd>
  98. </dl>
  99. </div>
  100. <h4>Package files</h4>
  101. <p>
  102. <span style="font-size:90%">
  103. <a href="http://localhost:6060/src/io/ioutil/ioutil.go">ioutil.go</a>
  104. <a href="http://localhost:6060/src/io/ioutil/tempfile.go">tempfile.go</a>
  105. </span>
  106. </p>
  107. </div><!-- .expanded -->
  108. </div><!-- #pkg-index -->
  109. <div id="pkg-callgraph" class="toggle" style="display: none">
  110. <div class="collapsed">
  111. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  112. </div> <!-- .expanded -->
  113. <div class="expanded">
  114. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  115. <p>
  116. In the call graph viewer below, each node
  117. is a function belonging to this package
  118. and its children are the functions it
  119. calls&mdash;perhaps dynamically.
  120. </p>
  121. <p>
  122. The root nodes are the entry points of the
  123. package: functions that may be called from
  124. outside the package.
  125. There may be non-exported or anonymous
  126. functions among them if they are called
  127. dynamically from another package.
  128. </p>
  129. <p>
  130. Click a node to visit that function's source code.
  131. From there you can visit its callers by
  132. clicking its declaring <code>func</code>
  133. token.
  134. </p>
  135. <p>
  136. Functions may be omitted if they were
  137. determined to be unreachable in the
  138. particular programs or tests that were
  139. analyzed.
  140. </p>
  141. <!-- Zero means show all package entry points. -->
  142. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  143. </div>
  144. </div> <!-- #pkg-callgraph -->
  145. <h2 id="pkg-variables">Variables</h2>
  146. <pre>var <span id="Discard">Discard</span> <a href="../index.html">io</a>.<a href="../index.html#Writer">Writer</a> = devNull(0)</pre>
  147. <p>
  148. Discard is an io.Writer on which all Write calls succeed
  149. without doing anything.
  150. </p>
  151. <h2 id="NopCloser">func <a href="http://localhost:6060/src/io/ioutil/ioutil.go?s=3643:3684#L112">NopCloser</a></h2>
  152. <pre>func NopCloser(r <a href="../index.html">io</a>.<a href="../index.html#Reader">Reader</a>) <a href="../index.html">io</a>.<a href="../index.html#ReadCloser">ReadCloser</a></pre>
  153. <p>
  154. NopCloser returns a ReadCloser with a no-op Close method wrapping
  155. the provided Reader r.
  156. </p>
  157. <h2 id="ReadAll">func <a href="http://localhost:6060/src/io/ioutil/ioutil.go?s=1147:1188#L31">ReadAll</a></h2>
  158. <pre>func ReadAll(r <a href="../index.html">io</a>.<a href="../index.html#Reader">Reader</a>) ([]<a href="../../builtin/index.html#byte">byte</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  159. <p>
  160. ReadAll reads from r until an error or EOF and returns the data it read.
  161. A successful call returns err == nil, not err == EOF. Because ReadAll is
  162. defined to read from src until EOF, it does not treat an EOF from Read
  163. as an error to be reported.
  164. </p>
  165. <div id="example_ReadAll" class="toggle">
  166. <div class="collapsed">
  167. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  168. </div>
  169. <div class="expanded">
  170. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  171. <p>Code:</p>
  172. <pre class="code">r := strings.NewReader(&#34;Go is a general-purpose language designed with systems programming in mind.&#34;)
  173. b, err := ioutil.ReadAll(r)
  174. if err != nil {
  175. log.Fatal(err)
  176. }
  177. fmt.Printf(&#34;%s&#34;, b)
  178. <span class="comment"></pre>
  179. <p>Output:</p>
  180. <pre class="output">Go is a general-purpose language designed with systems programming in mind.
  181. </pre>
  182. </div>
  183. </div>
  184. <h2 id="ReadDir">func <a href="http://localhost:6060/src/io/ioutil/ioutil.go?s=3220:3271#L90">ReadDir</a></h2>
  185. <pre>func ReadDir(dirname <a href="../../builtin/index.html#string">string</a>) ([]<a href="../../os/index.html">os</a>.<a href="../../os/index.html#FileInfo">FileInfo</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  186. <p>
  187. ReadDir reads the directory named by dirname and returns
  188. a list of directory entries sorted by filename.
  189. </p>
  190. <div id="example_ReadDir" class="toggle">
  191. <div class="collapsed">
  192. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  193. </div>
  194. <div class="expanded">
  195. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  196. <p>Code:</p>
  197. <pre class="code">
  198. files, err := ioutil.ReadDir(&#34;.&#34;)
  199. if err != nil {
  200. log.Fatal(err)
  201. }
  202. for _, file := range files {
  203. fmt.Println(file.Name())
  204. }
  205. </pre>
  206. </div>
  207. </div>
  208. <h2 id="ReadFile">func <a href="http://localhost:6060/src/io/ioutil/ioutil.go?s=1464:1510#L39">ReadFile</a></h2>
  209. <pre>func ReadFile(filename <a href="../../builtin/index.html#string">string</a>) ([]<a href="../../builtin/index.html#byte">byte</a>, <a href="../../builtin/index.html#error">error</a>)</pre>
  210. <p>
  211. ReadFile reads the file named by filename and returns the contents.
  212. A successful call returns err == nil, not err == EOF. Because ReadFile
  213. reads the whole file, it does not treat an EOF from Read as an error
  214. to be reported.
  215. </p>
  216. <h2 id="TempDir">func <a href="http://localhost:6060/src/io/ioutil/tempfile.go?s=2143:2200#L66">TempDir</a></h2>
  217. <pre>func TempDir(dir, prefix <a href="../../builtin/index.html#string">string</a>) (name <a href="../../builtin/index.html#string">string</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  218. <p>
  219. TempDir creates a new temporary directory in the directory dir
  220. with a name beginning with prefix and returns the path of the
  221. new directory. If dir is the empty string, TempDir uses the
  222. default directory for temporary files (see os.TempDir).
  223. Multiple programs calling TempDir simultaneously
  224. will not choose the same directory. It is the caller&#39;s responsibility
  225. to remove the directory when no longer needed.
  226. </p>
  227. <div id="example_TempDir" class="toggle">
  228. <div class="collapsed">
  229. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  230. </div>
  231. <div class="expanded">
  232. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  233. <p>Code:</p>
  234. <pre class="code">
  235. content := []byte(&#34;temporary file&#39;s content&#34;)
  236. dir, err := ioutil.TempDir(&#34;&#34;, &#34;example&#34;)
  237. if err != nil {
  238. log.Fatal(err)
  239. }
  240. defer os.RemoveAll(dir) <span class="comment">// clean up</span>
  241. tmpfn := filepath.Join(dir, &#34;tmpfile&#34;)
  242. if err := ioutil.WriteFile(tmpfn, content, 0666); err != nil {
  243. log.Fatal(err)
  244. }
  245. </pre>
  246. </div>
  247. </div>
  248. <h2 id="TempFile">func <a href="http://localhost:6060/src/io/ioutil/tempfile.go?s=1288:1345#L37">TempFile</a></h2>
  249. <pre>func TempFile(dir, prefix <a href="../../builtin/index.html#string">string</a>) (f *<a href="../../os/index.html">os</a>.<a href="../../os/index.html#File">File</a>, err <a href="../../builtin/index.html#error">error</a>)</pre>
  250. <p>
  251. TempFile creates a new temporary file in the directory dir
  252. with a name beginning with prefix, opens the file for reading
  253. and writing, and returns the resulting *os.File.
  254. If dir is the empty string, TempFile uses the default directory
  255. for temporary files (see os.TempDir).
  256. Multiple programs calling TempFile simultaneously
  257. will not choose the same file. The caller can use f.Name()
  258. to find the pathname of the file. It is the caller&#39;s responsibility
  259. to remove the file when no longer needed.
  260. </p>
  261. <div id="example_TempFile" class="toggle">
  262. <div class="collapsed">
  263. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  264. </div>
  265. <div class="expanded">
  266. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  267. <p>Code:</p>
  268. <pre class="code">
  269. content := []byte(&#34;temporary file&#39;s content&#34;)
  270. tmpfile, err := ioutil.TempFile(&#34;&#34;, &#34;example&#34;)
  271. if err != nil {
  272. log.Fatal(err)
  273. }
  274. defer os.Remove(tmpfile.Name()) <span class="comment">// clean up</span>
  275. if _, err := tmpfile.Write(content); err != nil {
  276. log.Fatal(err)
  277. }
  278. if err := tmpfile.Close(); err != nil {
  279. log.Fatal(err)
  280. }
  281. </pre>
  282. </div>
  283. </div>
  284. <h2 id="WriteFile">func <a href="http://localhost:6060/src/io/ioutil/ioutil.go?s=2520:2588#L66">WriteFile</a></h2>
  285. <pre>func WriteFile(filename <a href="../../builtin/index.html#string">string</a>, data []<a href="../../builtin/index.html#byte">byte</a>, perm <a href="../../os/index.html">os</a>.<a href="../../os/index.html#FileMode">FileMode</a>) <a href="../../builtin/index.html#error">error</a></pre>
  286. <p>
  287. WriteFile writes data to a file named by filename.
  288. If the file does not exist, WriteFile creates it with permissions perm;
  289. otherwise WriteFile truncates it before writing.
  290. </p>
  291. <div id="footer">
  292. Build version go1.6.<br>
  293. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  294. the content of this page is licensed under the
  295. Creative Commons Attribution 3.0 License,
  296. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  297. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  298. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  299. </div>
  300. </div><!-- .container -->
  301. </div><!-- #page -->
  302. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  303. <script type="text/javascript" src="../../../lib/godoc/jquery.js"></script>
  304. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.js"></script>
  305. <script type="text/javascript" src="../../../lib/godoc/jquery.treeview.edit.js"></script>
  306. <script type="text/javascript" src="../../../lib/godoc/godocs.js"></script>
  307. </body>
  308. </html>