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.

1945 lines
51 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>io - 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 io</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"</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. <dd><a href="index.html#pkg-subdirectories">Subdirectories</a></dd>
  59. </dl>
  60. </div>
  61. <!-- The package's Name is printed as title by the top-level template -->
  62. <div id="pkg-overview" class="toggleVisible">
  63. <div class="collapsed">
  64. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  65. </div>
  66. <div class="expanded">
  67. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  68. <p>
  69. Package io provides basic interfaces to I/O primitives.
  70. Its primary job is to wrap existing implementations of such primitives,
  71. such as those in package os, into shared public interfaces that
  72. abstract the functionality, plus some other related primitives.
  73. </p>
  74. <p>
  75. Because these interfaces and primitives wrap lower-level operations with
  76. various implementations, unless otherwise informed clients should not
  77. assume they are safe for parallel execution.
  78. </p>
  79. </div>
  80. </div>
  81. <div id="pkg-index" class="toggleVisible">
  82. <div class="collapsed">
  83. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  84. </div>
  85. <div class="expanded">
  86. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  87. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  88. <div id="manual-nav">
  89. <dl>
  90. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  91. <dd><a href="index.html#Copy">func Copy(dst Writer, src Reader) (written int64, err error)</a></dd>
  92. <dd><a href="index.html#CopyBuffer">func CopyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error)</a></dd>
  93. <dd><a href="index.html#CopyN">func CopyN(dst Writer, src Reader, n int64) (written int64, err error)</a></dd>
  94. <dd><a href="index.html#ReadAtLeast">func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)</a></dd>
  95. <dd><a href="index.html#ReadFull">func ReadFull(r Reader, buf []byte) (n int, err error)</a></dd>
  96. <dd><a href="index.html#WriteString">func WriteString(w Writer, s string) (n int, err error)</a></dd>
  97. <dd><a href="index.html#ByteReader">type ByteReader</a></dd>
  98. <dd><a href="index.html#ByteScanner">type ByteScanner</a></dd>
  99. <dd><a href="index.html#ByteWriter">type ByteWriter</a></dd>
  100. <dd><a href="index.html#Closer">type Closer</a></dd>
  101. <dd><a href="index.html#LimitedReader">type LimitedReader</a></dd>
  102. <dd>&nbsp; &nbsp; <a href="index.html#LimitedReader.Read">func (l *LimitedReader) Read(p []byte) (n int, err error)</a></dd>
  103. <dd><a href="index.html#PipeReader">type PipeReader</a></dd>
  104. <dd>&nbsp; &nbsp; <a href="index.html#Pipe">func Pipe() (*PipeReader, *PipeWriter)</a></dd>
  105. <dd>&nbsp; &nbsp; <a href="index.html#PipeReader.Close">func (r *PipeReader) Close() error</a></dd>
  106. <dd>&nbsp; &nbsp; <a href="index.html#PipeReader.CloseWithError">func (r *PipeReader) CloseWithError(err error) error</a></dd>
  107. <dd>&nbsp; &nbsp; <a href="index.html#PipeReader.Read">func (r *PipeReader) Read(data []byte) (n int, err error)</a></dd>
  108. <dd><a href="index.html#PipeWriter">type PipeWriter</a></dd>
  109. <dd>&nbsp; &nbsp; <a href="index.html#PipeWriter.Close">func (w *PipeWriter) Close() error</a></dd>
  110. <dd>&nbsp; &nbsp; <a href="index.html#PipeWriter.CloseWithError">func (w *PipeWriter) CloseWithError(err error) error</a></dd>
  111. <dd>&nbsp; &nbsp; <a href="index.html#PipeWriter.Write">func (w *PipeWriter) Write(data []byte) (n int, err error)</a></dd>
  112. <dd><a href="index.html#ReadCloser">type ReadCloser</a></dd>
  113. <dd><a href="index.html#ReadSeeker">type ReadSeeker</a></dd>
  114. <dd><a href="index.html#ReadWriteCloser">type ReadWriteCloser</a></dd>
  115. <dd><a href="index.html#ReadWriteSeeker">type ReadWriteSeeker</a></dd>
  116. <dd><a href="index.html#ReadWriter">type ReadWriter</a></dd>
  117. <dd><a href="index.html#Reader">type Reader</a></dd>
  118. <dd>&nbsp; &nbsp; <a href="index.html#LimitReader">func LimitReader(r Reader, n int64) Reader</a></dd>
  119. <dd>&nbsp; &nbsp; <a href="index.html#MultiReader">func MultiReader(readers ...Reader) Reader</a></dd>
  120. <dd>&nbsp; &nbsp; <a href="index.html#TeeReader">func TeeReader(r Reader, w Writer) Reader</a></dd>
  121. <dd><a href="index.html#ReaderAt">type ReaderAt</a></dd>
  122. <dd><a href="index.html#ReaderFrom">type ReaderFrom</a></dd>
  123. <dd><a href="index.html#RuneReader">type RuneReader</a></dd>
  124. <dd><a href="index.html#RuneScanner">type RuneScanner</a></dd>
  125. <dd><a href="index.html#SectionReader">type SectionReader</a></dd>
  126. <dd>&nbsp; &nbsp; <a href="index.html#NewSectionReader">func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader</a></dd>
  127. <dd>&nbsp; &nbsp; <a href="index.html#SectionReader.Read">func (s *SectionReader) Read(p []byte) (n int, err error)</a></dd>
  128. <dd>&nbsp; &nbsp; <a href="index.html#SectionReader.ReadAt">func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error)</a></dd>
  129. <dd>&nbsp; &nbsp; <a href="index.html#SectionReader.Seek">func (s *SectionReader) Seek(offset int64, whence int) (int64, error)</a></dd>
  130. <dd>&nbsp; &nbsp; <a href="index.html#SectionReader.Size">func (s *SectionReader) Size() int64</a></dd>
  131. <dd><a href="index.html#Seeker">type Seeker</a></dd>
  132. <dd><a href="index.html#WriteCloser">type WriteCloser</a></dd>
  133. <dd><a href="index.html#WriteSeeker">type WriteSeeker</a></dd>
  134. <dd><a href="index.html#Writer">type Writer</a></dd>
  135. <dd>&nbsp; &nbsp; <a href="index.html#MultiWriter">func MultiWriter(writers ...Writer) Writer</a></dd>
  136. <dd><a href="index.html#WriterAt">type WriterAt</a></dd>
  137. <dd><a href="index.html#WriterTo">type WriterTo</a></dd>
  138. </dl>
  139. </div><!-- #manual-nav -->
  140. <div id="pkg-examples">
  141. <h4>Examples</h4>
  142. <dl>
  143. <dd><a class="exampleLink" href="index.html#example_Copy">Copy</a></dd>
  144. <dd><a class="exampleLink" href="index.html#example_CopyBuffer">CopyBuffer</a></dd>
  145. <dd><a class="exampleLink" href="index.html#example_CopyN">CopyN</a></dd>
  146. <dd><a class="exampleLink" href="index.html#example_LimitReader">LimitReader</a></dd>
  147. <dd><a class="exampleLink" href="index.html#example_MultiReader">MultiReader</a></dd>
  148. <dd><a class="exampleLink" href="index.html#example_MultiWriter">MultiWriter</a></dd>
  149. <dd><a class="exampleLink" href="index.html#example_ReadAtLeast">ReadAtLeast</a></dd>
  150. <dd><a class="exampleLink" href="index.html#example_ReadFull">ReadFull</a></dd>
  151. <dd><a class="exampleLink" href="index.html#example_SectionReader">SectionReader</a></dd>
  152. <dd><a class="exampleLink" href="index.html#example_SectionReader_ReadAt">SectionReader.ReadAt</a></dd>
  153. <dd><a class="exampleLink" href="index.html#example_SectionReader_Seek">SectionReader.Seek</a></dd>
  154. <dd><a class="exampleLink" href="index.html#example_TeeReader">TeeReader</a></dd>
  155. <dd><a class="exampleLink" href="index.html#example_WriteString">WriteString</a></dd>
  156. </dl>
  157. </div>
  158. <h4>Package files</h4>
  159. <p>
  160. <span style="font-size:90%">
  161. <a href="http://localhost:6060/src/io/io.go">io.go</a>
  162. <a href="http://localhost:6060/src/io/multi.go">multi.go</a>
  163. <a href="http://localhost:6060/src/io/pipe.go">pipe.go</a>
  164. </span>
  165. </p>
  166. </div><!-- .expanded -->
  167. </div><!-- #pkg-index -->
  168. <div id="pkg-callgraph" class="toggle" style="display: none">
  169. <div class="collapsed">
  170. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  171. </div> <!-- .expanded -->
  172. <div class="expanded">
  173. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  174. <p>
  175. In the call graph viewer below, each node
  176. is a function belonging to this package
  177. and its children are the functions it
  178. calls&mdash;perhaps dynamically.
  179. </p>
  180. <p>
  181. The root nodes are the entry points of the
  182. package: functions that may be called from
  183. outside the package.
  184. There may be non-exported or anonymous
  185. functions among them if they are called
  186. dynamically from another package.
  187. </p>
  188. <p>
  189. Click a node to visit that function's source code.
  190. From there you can visit its callers by
  191. clicking its declaring <code>func</code>
  192. token.
  193. </p>
  194. <p>
  195. Functions may be omitted if they were
  196. determined to be unreachable in the
  197. particular programs or tests that were
  198. analyzed.
  199. </p>
  200. <!-- Zero means show all package entry points. -->
  201. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  202. </div>
  203. </div> <!-- #pkg-callgraph -->
  204. <h2 id="pkg-variables">Variables</h2>
  205. <pre>var <span id="EOF">EOF</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;EOF&#34;)</pre>
  206. <p>
  207. EOF is the error returned by Read when no more input is available.
  208. Functions should return EOF only to signal a graceful end of input.
  209. If the EOF occurs unexpectedly in a structured data stream,
  210. the appropriate error is either ErrUnexpectedEOF or some other error
  211. giving more detail.
  212. </p>
  213. <pre>var <span id="ErrClosedPipe">ErrClosedPipe</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;io: read/write on closed pipe&#34;)</pre>
  214. <p>
  215. ErrClosedPipe is the error used for read or write operations on a closed pipe.
  216. </p>
  217. <pre>var <span id="ErrNoProgress">ErrNoProgress</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;multiple Read calls return no data or error&#34;)</pre>
  218. <p>
  219. ErrNoProgress is returned by some clients of an io.Reader when
  220. many calls to Read have failed to return any data or error,
  221. usually the sign of a broken io.Reader implementation.
  222. </p>
  223. <pre>var <span id="ErrShortBuffer">ErrShortBuffer</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;short buffer&#34;)</pre>
  224. <p>
  225. ErrShortBuffer means that a read required a longer buffer than was provided.
  226. </p>
  227. <pre>var <span id="ErrShortWrite">ErrShortWrite</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;short write&#34;)</pre>
  228. <p>
  229. ErrShortWrite means that a write accepted fewer bytes than requested
  230. but failed to return an explicit error.
  231. </p>
  232. <pre>var <span id="ErrUnexpectedEOF">ErrUnexpectedEOF</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;unexpected EOF&#34;)</pre>
  233. <p>
  234. ErrUnexpectedEOF means that EOF was encountered in the
  235. middle of reading a fixed-size block or data structure.
  236. </p>
  237. <h2 id="Copy">func <a href="http://localhost:6060/src/io/io.go?s=12235:12295#L339">Copy</a></h2>
  238. <pre>func Copy(dst <a href="index.html#Writer">Writer</a>, src <a href="index.html#Reader">Reader</a>) (written <a href="../builtin/index.html#int64">int64</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  239. <p>
  240. Copy copies from src to dst until either EOF is reached
  241. on src or an error occurs. It returns the number of bytes
  242. copied and the first error encountered while copying, if any.
  243. </p>
  244. <p>
  245. A successful Copy returns err == nil, not err == EOF.
  246. Because Copy is defined to read from src until EOF, it does
  247. not treat an EOF from Read as an error to be reported.
  248. </p>
  249. <p>
  250. If src implements the WriterTo interface,
  251. the copy is implemented by calling src.WriteTo(dst).
  252. Otherwise, if dst implements the ReaderFrom interface,
  253. the copy is implemented by calling dst.ReadFrom(src).
  254. </p>
  255. <div id="example_Copy" class="toggle">
  256. <div class="collapsed">
  257. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  258. </div>
  259. <div class="expanded">
  260. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  261. <p>Code:</p>
  262. <pre class="code">r := strings.NewReader(&#34;some io.Reader stream to be read\n&#34;)
  263. if _, err := io.Copy(os.Stdout, r); err != nil {
  264. log.Fatal(err)
  265. }
  266. <span class="comment"></pre>
  267. <p>Output:</p>
  268. <pre class="output">some io.Reader stream to be read
  269. </pre>
  270. </div>
  271. </div>
  272. <h2 id="CopyBuffer">func <a href="http://localhost:6060/src/io/io.go?s=12575:12653#L347">CopyBuffer</a></h2>
  273. <pre>func CopyBuffer(dst <a href="index.html#Writer">Writer</a>, src <a href="index.html#Reader">Reader</a>, buf []<a href="../builtin/index.html#byte">byte</a>) (written <a href="../builtin/index.html#int64">int64</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  274. <p>
  275. CopyBuffer is identical to Copy except that it stages through the
  276. provided buffer (if one is required) rather than allocating a
  277. temporary one. If buf is nil, one is allocated; otherwise if it has
  278. zero length, CopyBuffer panics.
  279. </p>
  280. <div id="example_CopyBuffer" class="toggle">
  281. <div class="collapsed">
  282. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  283. </div>
  284. <div class="expanded">
  285. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  286. <p>Code:</p>
  287. <pre class="code">r1 := strings.NewReader(&#34;first reader\n&#34;)
  288. r2 := strings.NewReader(&#34;second reader\n&#34;)
  289. buf := make([]byte, 8)
  290. <span class="comment">// buf is used here...</span>
  291. if _, err := io.CopyBuffer(os.Stdout, r1, buf); err != nil {
  292. log.Fatal(err)
  293. }
  294. <span class="comment">// ... reused here also. No need to allocate an extra buffer.</span>
  295. if _, err := io.CopyBuffer(os.Stdout, r2, buf); err != nil {
  296. log.Fatal(err)
  297. }
  298. <span class="comment"></pre>
  299. <p>Output:</p>
  300. <pre class="output">first reader
  301. second reader
  302. </pre>
  303. </div>
  304. </div>
  305. <h2 id="CopyN">func <a href="http://localhost:6060/src/io/io.go?s=11389:11459#L315">CopyN</a></h2>
  306. <pre>func CopyN(dst <a href="index.html#Writer">Writer</a>, src <a href="index.html#Reader">Reader</a>, n <a href="../builtin/index.html#int64">int64</a>) (written <a href="../builtin/index.html#int64">int64</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  307. <p>
  308. CopyN copies n bytes (or until an error) from src to dst.
  309. It returns the number of bytes copied and the earliest
  310. error encountered while copying.
  311. On return, written == n if and only if err == nil.
  312. </p>
  313. <p>
  314. If dst implements the ReaderFrom interface,
  315. the copy is implemented using it.
  316. </p>
  317. <div id="example_CopyN" class="toggle">
  318. <div class="collapsed">
  319. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  320. </div>
  321. <div class="expanded">
  322. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  323. <p>Code:</p>
  324. <pre class="code">r := strings.NewReader(&#34;some io.Reader stream to be read&#34;)
  325. if _, err := io.CopyN(os.Stdout, r, 5); err != nil {
  326. log.Fatal(err)
  327. }
  328. <span class="comment"></pre>
  329. <p>Output:</p>
  330. <pre class="output">some
  331. </pre>
  332. </div>
  333. </div>
  334. <h2 id="ReadAtLeast">func <a href="http://localhost:6060/src/io/io.go?s=10351:10417#L281">ReadAtLeast</a></h2>
  335. <pre>func ReadAtLeast(r <a href="index.html#Reader">Reader</a>, buf []<a href="../builtin/index.html#byte">byte</a>, min <a href="../builtin/index.html#int">int</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  336. <p>
  337. ReadAtLeast reads from r into buf until it has read at least min bytes.
  338. It returns the number of bytes copied and an error if fewer bytes were read.
  339. The error is EOF only if no bytes were read.
  340. If an EOF happens after reading fewer than min bytes,
  341. ReadAtLeast returns ErrUnexpectedEOF.
  342. If min is greater than the length of buf, ReadAtLeast returns ErrShortBuffer.
  343. On return, n &gt;= min if and only if err == nil.
  344. </p>
  345. <div id="example_ReadAtLeast" class="toggle">
  346. <div class="collapsed">
  347. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  348. </div>
  349. <div class="expanded">
  350. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  351. <p>Code:</p>
  352. <pre class="code">r := strings.NewReader(&#34;some io.Reader stream to be read\n&#34;)
  353. buf := make([]byte, 33)
  354. if _, err := io.ReadAtLeast(r, buf, 4); err != nil {
  355. log.Fatal(err)
  356. }
  357. fmt.Printf(&#34;%s\n&#34;, buf)
  358. <span class="comment">// buffer smaller than minimal read size.</span>
  359. shortBuf := make([]byte, 3)
  360. if _, err := io.ReadAtLeast(r, shortBuf, 4); err != nil {
  361. fmt.Println(&#34;error:&#34;, err)
  362. }
  363. <span class="comment">// minimal read size bigger than io.Reader stream</span>
  364. longBuf := make([]byte, 64)
  365. if _, err := io.ReadAtLeast(r, longBuf, 64); err != nil {
  366. fmt.Println(&#34;error:&#34;, err)
  367. }
  368. <span class="comment"></pre>
  369. <p>Output:</p>
  370. <pre class="output">some io.Reader stream to be read
  371. error: short buffer
  372. error: EOF
  373. </pre>
  374. </div>
  375. </div>
  376. <h2 id="ReadFull">func <a href="http://localhost:6060/src/io/io.go?s=10995:11049#L304">ReadFull</a></h2>
  377. <pre>func ReadFull(r <a href="index.html#Reader">Reader</a>, buf []<a href="../builtin/index.html#byte">byte</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  378. <p>
  379. ReadFull reads exactly len(buf) bytes from r into buf.
  380. It returns the number of bytes copied and an error if fewer bytes were read.
  381. The error is EOF only if no bytes were read.
  382. If an EOF happens after reading some but not all the bytes,
  383. ReadFull returns ErrUnexpectedEOF.
  384. On return, n == len(buf) if and only if err == nil.
  385. </p>
  386. <div id="example_ReadFull" class="toggle">
  387. <div class="collapsed">
  388. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  389. </div>
  390. <div class="expanded">
  391. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  392. <p>Code:</p>
  393. <pre class="code">r := strings.NewReader(&#34;some io.Reader stream to be read\n&#34;)
  394. buf := make([]byte, 4)
  395. if _, err := io.ReadFull(r, buf); err != nil {
  396. log.Fatal(err)
  397. }
  398. fmt.Printf(&#34;%s\n&#34;, buf)
  399. <span class="comment">// minimal read size bigger than io.Reader stream</span>
  400. longBuf := make([]byte, 64)
  401. if _, err := io.ReadFull(r, longBuf); err != nil {
  402. fmt.Println(&#34;error:&#34;, err)
  403. }
  404. <span class="comment"></pre>
  405. <p>Output:</p>
  406. <pre class="output">some
  407. error: unexpected EOF
  408. </pre>
  409. </div>
  410. </div>
  411. <h2 id="WriteString">func <a href="http://localhost:6060/src/io/io.go?s=9764:9819#L267">WriteString</a></h2>
  412. <pre>func WriteString(w <a href="index.html#Writer">Writer</a>, s <a href="../builtin/index.html#string">string</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  413. <p>
  414. WriteString writes the contents of the string s to w, which accepts a slice of bytes.
  415. If w implements a WriteString method, it is invoked directly.
  416. </p>
  417. <div id="example_WriteString" class="toggle">
  418. <div class="collapsed">
  419. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  420. </div>
  421. <div class="expanded">
  422. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  423. <p>Code:</p>
  424. <pre class="code">io.WriteString(os.Stdout, &#34;Hello World&#34;)
  425. <span class="comment"></pre>
  426. <p>Output:</p>
  427. <pre class="output">Hello World
  428. </pre>
  429. </div>
  430. </div>
  431. <h2 id="ByteReader">type <a href="http://localhost:6060/src/io/io.go?s=8263:8324#L218">ByteReader</a></h2>
  432. <pre>type ByteReader interface {
  433. ReadByte() (c <a href="../builtin/index.html#byte">byte</a>, err <a href="../builtin/index.html#error">error</a>)
  434. }</pre>
  435. <p>
  436. ByteReader is the interface that wraps the ReadByte method.
  437. </p>
  438. <p>
  439. ReadByte reads and returns the next byte from the input.
  440. </p>
  441. <h2 id="ByteScanner">type <a href="http://localhost:6060/src/io/io.go?s=8625:8687#L229">ByteScanner</a></h2>
  442. <pre>type ByteScanner interface {
  443. <a href="index.html#ByteReader">ByteReader</a>
  444. UnreadByte() <a href="../builtin/index.html#error">error</a>
  445. }</pre>
  446. <p>
  447. ByteScanner is the interface that adds the UnreadByte method to the
  448. basic ReadByte method.
  449. </p>
  450. <p>
  451. UnreadByte causes the next call to ReadByte to return the same byte
  452. as the previous call to ReadByte.
  453. It may be an error to call UnreadByte twice without an intervening
  454. call to ReadByte.
  455. </p>
  456. <h2 id="ByteWriter">type <a href="http://localhost:6060/src/io/io.go?s=8753:8807#L235">ByteWriter</a></h2>
  457. <pre>type ByteWriter interface {
  458. WriteByte(c <a href="../builtin/index.html#byte">byte</a>) <a href="../builtin/index.html#error">error</a>
  459. }</pre>
  460. <p>
  461. ByteWriter is the interface that wraps the WriteByte method.
  462. </p>
  463. <h2 id="Closer">type <a href="http://localhost:6060/src/io/io.go?s=3853:3893#L81">Closer</a></h2>
  464. <pre>type Closer interface {
  465. Close() <a href="../builtin/index.html#error">error</a>
  466. }</pre>
  467. <p>
  468. Closer is the interface that wraps the basic Close method.
  469. </p>
  470. <p>
  471. The behavior of Close after the first call is undefined.
  472. Specific implementations may document their own behavior.
  473. </p>
  474. <h2 id="LimitedReader">type <a href="http://localhost:6060/src/io/io.go?s=14016:14109#L404">LimitedReader</a></h2>
  475. <pre>type LimitedReader struct {
  476. R <a href="index.html#Reader">Reader</a> <span class="comment">// underlying reader</span>
  477. N <a href="../builtin/index.html#int64">int64</a> <span class="comment">// max bytes remaining</span>
  478. }</pre>
  479. <p>
  480. A LimitedReader reads from R but limits the amount of
  481. data returned to just N bytes. Each call to Read
  482. updates N to reflect the new amount remaining.
  483. </p>
  484. <h3 id="LimitedReader.Read">func (*LimitedReader) <a href="http://localhost:6060/src/io/io.go?s=14111:14168#L409">Read</a></h3>
  485. <pre>func (l *<a href="index.html#LimitedReader">LimitedReader</a>) Read(p []<a href="../builtin/index.html#byte">byte</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  486. <h2 id="PipeReader">type <a href="http://localhost:6060/src/io/pipe.go?s=2308:2343#L114">PipeReader</a></h2>
  487. <pre>type PipeReader struct {
  488. <span class="comment">// contains filtered or unexported fields</span>
  489. }</pre>
  490. <p>
  491. A PipeReader is the read half of a pipe.
  492. </p>
  493. <h3 id="Pipe">func <a href="http://localhost:6060/src/io/pipe.go?s=4486:4524#L179">Pipe</a></h3>
  494. <pre>func Pipe() (*<a href="index.html#PipeReader">PipeReader</a>, *<a href="index.html#PipeWriter">PipeWriter</a>)</pre>
  495. <p>
  496. Pipe creates a synchronous in-memory pipe.
  497. It can be used to connect code expecting an io.Reader
  498. with code expecting an io.Writer.
  499. Reads on one end are matched with writes on the other,
  500. copying data directly between the two; there is no internal buffering.
  501. It is safe to call Read and Write in parallel with each other or with
  502. Close. Close will complete once pending I/O is done. Parallel calls to
  503. Read, and parallel calls to Write, are also safe:
  504. the individual calls will be gated sequentially.
  505. </p>
  506. <h3 id="PipeReader.Close">func (*PipeReader) <a href="http://localhost:6060/src/io/pipe.go?s=2791:2825#L129">Close</a></h3>
  507. <pre>func (r *<a href="index.html#PipeReader">PipeReader</a>) Close() <a href="../builtin/index.html#error">error</a></pre>
  508. <p>
  509. Close closes the reader; subsequent writes to the
  510. write half of the pipe will return the error ErrClosedPipe.
  511. </p>
  512. <h3 id="PipeReader.CloseWithError">func (*PipeReader) <a href="http://localhost:6060/src/io/pipe.go?s=2976:3028#L135">CloseWithError</a></h3>
  513. <pre>func (r *<a href="index.html#PipeReader">PipeReader</a>) CloseWithError(err <a href="../builtin/index.html#error">error</a>) <a href="../builtin/index.html#error">error</a></pre>
  514. <p>
  515. CloseWithError closes the reader; subsequent writes
  516. to the write half of the pipe will return the error err.
  517. </p>
  518. <h3 id="PipeReader.Read">func (*PipeReader) <a href="http://localhost:6060/src/io/pipe.go?s=2589:2646#L123">Read</a></h3>
  519. <pre>func (r *<a href="index.html#PipeReader">PipeReader</a>) Read(data []<a href="../builtin/index.html#byte">byte</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  520. <p>
  521. Read implements the standard Read interface:
  522. it reads data from the pipe, blocking until a writer
  523. arrives or the write end is closed.
  524. If the write end is closed with an error, that error is
  525. returned as err; otherwise err is EOF.
  526. </p>
  527. <h2 id="PipeWriter">type <a href="http://localhost:6060/src/io/pipe.go?s=3108:3143#L141">PipeWriter</a></h2>
  528. <pre>type PipeWriter struct {
  529. <span class="comment">// contains filtered or unexported fields</span>
  530. }</pre>
  531. <p>
  532. A PipeWriter is the write half of a pipe.
  533. </p>
  534. <h3 id="PipeWriter.Close">func (*PipeWriter) <a href="http://localhost:6060/src/io/pipe.go?s=3611:3645#L156">Close</a></h3>
  535. <pre>func (w *<a href="index.html#PipeWriter">PipeWriter</a>) Close() <a href="../builtin/index.html#error">error</a></pre>
  536. <p>
  537. Close closes the writer; subsequent reads from the
  538. read half of the pipe will return no bytes and EOF.
  539. </p>
  540. <h3 id="PipeWriter.CloseWithError">func (*PipeWriter) <a href="http://localhost:6060/src/io/pipe.go?s=3875:3927#L165">CloseWithError</a></h3>
  541. <pre>func (w *<a href="index.html#PipeWriter">PipeWriter</a>) CloseWithError(err <a href="../builtin/index.html#error">error</a>) <a href="../builtin/index.html#error">error</a></pre>
  542. <p>
  543. CloseWithError closes the writer; subsequent reads from the
  544. read half of the pipe will return no bytes and the error err,
  545. or EOF if err is nil.
  546. </p>
  547. <p>
  548. CloseWithError always returns nil.
  549. </p>
  550. <h3 id="PipeWriter.Write">func (*PipeWriter) <a href="http://localhost:6060/src/io/pipe.go?s=3414:3472#L150">Write</a></h3>
  551. <pre>func (w *<a href="index.html#PipeWriter">PipeWriter</a>) Write(data []<a href="../builtin/index.html#byte">byte</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  552. <p>
  553. Write implements the standard Write interface:
  554. it writes data to the pipe, blocking until readers
  555. have consumed all the data or the read end is closed.
  556. If the read end is closed with an error, that err is
  557. returned as err; otherwise err is ErrClosedPipe.
  558. </p>
  559. <h2 id="ReadCloser">type <a href="http://localhost:6060/src/io/io.go?s=4757:4802#L107">ReadCloser</a></h2>
  560. <pre>type ReadCloser interface {
  561. <a href="index.html#Reader">Reader</a>
  562. <a href="index.html#Closer">Closer</a>
  563. }</pre>
  564. <p>
  565. ReadCloser is the interface that groups the basic Read and Close methods.
  566. </p>
  567. <h2 id="ReadSeeker">type <a href="http://localhost:6060/src/io/io.go?s=5156:5201#L126">ReadSeeker</a></h2>
  568. <pre>type ReadSeeker interface {
  569. <a href="index.html#Reader">Reader</a>
  570. <a href="index.html#Seeker">Seeker</a>
  571. }</pre>
  572. <p>
  573. ReadSeeker is the interface that groups the basic Read and Seek methods.
  574. </p>
  575. <h2 id="ReadWriteCloser">type <a href="http://localhost:6060/src/io/io.go?s=5020:5078#L119">ReadWriteCloser</a></h2>
  576. <pre>type ReadWriteCloser interface {
  577. <a href="index.html#Reader">Reader</a>
  578. <a href="index.html#Writer">Writer</a>
  579. <a href="index.html#Closer">Closer</a>
  580. }</pre>
  581. <p>
  582. ReadWriteCloser is the interface that groups the basic Read, Write and Close methods.
  583. </p>
  584. <h2 id="ReadWriteSeeker">type <a href="http://localhost:6060/src/io/io.go?s=5417:5475#L138">ReadWriteSeeker</a></h2>
  585. <pre>type ReadWriteSeeker interface {
  586. <a href="index.html#Reader">Reader</a>
  587. <a href="index.html#Writer">Writer</a>
  588. <a href="index.html#Seeker">Seeker</a>
  589. }</pre>
  590. <p>
  591. ReadWriteSeeker is the interface that groups the basic Read, Write and Seek methods.
  592. </p>
  593. <h2 id="ReadWriter">type <a href="http://localhost:6060/src/io/io.go?s=4633:4678#L101">ReadWriter</a></h2>
  594. <pre>type ReadWriter interface {
  595. <a href="index.html#Reader">Reader</a>
  596. <a href="index.html#Writer">Writer</a>
  597. }</pre>
  598. <p>
  599. ReadWriter is the interface that groups the basic Read and Write methods.
  600. </p>
  601. <h2 id="Reader">type <a href="http://localhost:6060/src/io/io.go?s=3113:3173#L60">Reader</a></h2>
  602. <pre>type Reader interface {
  603. Read(p []<a href="../builtin/index.html#byte">byte</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)
  604. }</pre>
  605. <p>
  606. Reader is the interface that wraps the basic Read method.
  607. </p>
  608. <p>
  609. Read reads up to len(p) bytes into p. It returns the number of bytes
  610. read (0 &lt;= n &lt;= len(p)) and any error encountered. Even if Read
  611. returns n &lt; len(p), it may use all of p as scratch space during the call.
  612. If some data is available but not len(p) bytes, Read conventionally
  613. returns what is available instead of waiting for more.
  614. </p>
  615. <p>
  616. When Read encounters an error or end-of-file condition after
  617. successfully reading n &gt; 0 bytes, it returns the number of
  618. bytes read. It may return the (non-nil) error from the same call
  619. or return the error (and n == 0) from a subsequent call.
  620. An instance of this general case is that a Reader returning
  621. a non-zero number of bytes at the end of the input stream may
  622. return either err == EOF or err == nil. The next Read should
  623. return 0, EOF.
  624. </p>
  625. <p>
  626. Callers should always process the n &gt; 0 bytes returned before
  627. considering the error err. Doing so correctly handles I/O errors
  628. that happen after reading some bytes and also both of the
  629. allowed EOF behaviors.
  630. </p>
  631. <p>
  632. Implementations of Read are discouraged from returning a
  633. zero byte count with a nil error, except when len(p) == 0.
  634. Callers should treat a return of 0 and nil as indicating that
  635. nothing happened; in particular it does not indicate EOF.
  636. </p>
  637. <p>
  638. Implementations must not retain p.
  639. </p>
  640. <h3 id="LimitReader">func <a href="http://localhost:6060/src/io/io.go?s=13781:13823#L399">LimitReader</a></h3>
  641. <pre>func LimitReader(r <a href="index.html#Reader">Reader</a>, n <a href="../builtin/index.html#int64">int64</a>) <a href="index.html#Reader">Reader</a></pre>
  642. <p>
  643. LimitReader returns a Reader that reads from r
  644. but stops with EOF after n bytes.
  645. The underlying implementation is a *LimitedReader.
  646. </p>
  647. <div id="example_LimitReader" class="toggle">
  648. <div class="collapsed">
  649. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  650. </div>
  651. <div class="expanded">
  652. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  653. <p>Code:</p>
  654. <pre class="code">r := strings.NewReader(&#34;some io.Reader stream to be read\n&#34;)
  655. lr := io.LimitReader(r, 4)
  656. if _, err := io.Copy(os.Stdout, lr); err != nil {
  657. log.Fatal(err)
  658. }
  659. <span class="comment"></pre>
  660. <p>Output:</p>
  661. <pre class="output">some
  662. </pre>
  663. </div>
  664. </div>
  665. <h3 id="MultiReader">func <a href="http://localhost:6060/src/io/multi.go?s=830:872#L21">MultiReader</a></h3>
  666. <pre>func MultiReader(readers ...<a href="index.html#Reader">Reader</a>) <a href="index.html#Reader">Reader</a></pre>
  667. <p>
  668. MultiReader returns a Reader that&#39;s the logical concatenation of
  669. the provided input readers. They&#39;re read sequentially. Once all
  670. inputs have returned EOF, Read will return EOF. If any of the readers
  671. return a non-nil, non-EOF error, Read will return that error.
  672. </p>
  673. <div id="example_MultiReader" class="toggle">
  674. <div class="collapsed">
  675. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  676. </div>
  677. <div class="expanded">
  678. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  679. <p>Code:</p>
  680. <pre class="code">r1 := strings.NewReader(&#34;first reader &#34;)
  681. r2 := strings.NewReader(&#34;second reader &#34;)
  682. r3 := strings.NewReader(&#34;third reader\n&#34;)
  683. r := io.MultiReader(r1, r2, r3)
  684. if _, err := io.Copy(os.Stdout, r); err != nil {
  685. log.Fatal(err)
  686. }
  687. <span class="comment"></pre>
  688. <p>Output:</p>
  689. <pre class="output">first reader second reader third reader
  690. </pre>
  691. </div>
  692. </div>
  693. <h3 id="TeeReader">func <a href="http://localhost:6060/src/io/io.go?s=16109:16150#L493">TeeReader</a></h3>
  694. <pre>func TeeReader(r <a href="index.html#Reader">Reader</a>, w <a href="index.html#Writer">Writer</a>) <a href="index.html#Reader">Reader</a></pre>
  695. <p>
  696. TeeReader returns a Reader that writes to w what it reads from r.
  697. All reads from r performed through it are matched with
  698. corresponding writes to w. There is no internal buffering -
  699. the write must complete before the read completes.
  700. Any error encountered while writing is reported as a read error.
  701. </p>
  702. <div id="example_TeeReader" class="toggle">
  703. <div class="collapsed">
  704. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  705. </div>
  706. <div class="expanded">
  707. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  708. <p>Code:</p>
  709. <pre class="code">r := strings.NewReader(&#34;some io.Reader stream to be read\n&#34;)
  710. var buf bytes.Buffer
  711. tee := io.TeeReader(r, &amp;buf)
  712. printall := func(r io.Reader) {
  713. b, err := ioutil.ReadAll(r)
  714. if err != nil {
  715. log.Fatal(err)
  716. }
  717. fmt.Printf(&#34;%s&#34;, b)
  718. }
  719. printall(tee)
  720. printall(&amp;buf)
  721. <span class="comment"></pre>
  722. <p>Output:</p>
  723. <pre class="output">some io.Reader stream to be read
  724. some io.Reader stream to be read
  725. </pre>
  726. </div>
  727. </div>
  728. <h2 id="ReaderAt">type <a href="http://localhost:6060/src/io/io.go?s=7328:7403#L192">ReaderAt</a></h2>
  729. <pre>type ReaderAt interface {
  730. ReadAt(p []<a href="../builtin/index.html#byte">byte</a>, off <a href="../builtin/index.html#int64">int64</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)
  731. }</pre>
  732. <p>
  733. ReaderAt is the interface that wraps the basic ReadAt method.
  734. </p>
  735. <p>
  736. ReadAt reads len(p) bytes into p starting at offset off in the
  737. underlying input source. It returns the number of bytes
  738. read (0 &lt;= n &lt;= len(p)) and any error encountered.
  739. </p>
  740. <p>
  741. When ReadAt returns n &lt; len(p), it returns a non-nil error
  742. explaining why more bytes were not returned. In this respect,
  743. ReadAt is stricter than Read.
  744. </p>
  745. <p>
  746. Even if ReadAt returns n &lt; len(p), it may use all of p as scratch
  747. space during the call. If some data is available but not len(p) bytes,
  748. ReadAt blocks until either all the data is available or an error occurs.
  749. In this respect ReadAt is different from Read.
  750. </p>
  751. <p>
  752. If the n = len(p) bytes returned by ReadAt are at the end of the
  753. input source, ReadAt may return either err == EOF or err == nil.
  754. </p>
  755. <p>
  756. If ReadAt is reading from an input source with a seek offset,
  757. ReadAt should not affect nor be affected by the underlying
  758. seek offset.
  759. </p>
  760. <p>
  761. Clients of ReadAt can execute parallel ReadAt calls on the
  762. same input source.
  763. </p>
  764. <p>
  765. Implementations must not retain p.
  766. </p>
  767. <h2 id="ReaderFrom">type <a href="http://localhost:6060/src/io/io.go?s=5771:5841#L151">ReaderFrom</a></h2>
  768. <pre>type ReaderFrom interface {
  769. ReadFrom(r <a href="index.html#Reader">Reader</a>) (n <a href="../builtin/index.html#int64">int64</a>, err <a href="../builtin/index.html#error">error</a>)
  770. }</pre>
  771. <p>
  772. ReaderFrom is the interface that wraps the ReadFrom method.
  773. </p>
  774. <p>
  775. ReadFrom reads data from r until EOF or error.
  776. The return value n is the number of bytes read.
  777. Any error except io.EOF encountered during the read is also returned.
  778. </p>
  779. <p>
  780. The Copy function uses ReaderFrom if available.
  781. </p>
  782. <h2 id="RuneReader">type <a href="http://localhost:6060/src/io/io.go?s=9031:9102#L244">RuneReader</a></h2>
  783. <pre>type RuneReader interface {
  784. ReadRune() (r <a href="../builtin/index.html#rune">rune</a>, size <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)
  785. }</pre>
  786. <p>
  787. RuneReader is the interface that wraps the ReadRune method.
  788. </p>
  789. <p>
  790. ReadRune reads a single UTF-8 encoded Unicode character
  791. and returns the rune and its size in bytes. If no character is
  792. available, err will be set.
  793. </p>
  794. <h2 id="RuneScanner">type <a href="http://localhost:6060/src/io/io.go?s=9403:9465#L255">RuneScanner</a></h2>
  795. <pre>type RuneScanner interface {
  796. <a href="index.html#RuneReader">RuneReader</a>
  797. UnreadRune() <a href="../builtin/index.html#error">error</a>
  798. }</pre>
  799. <p>
  800. RuneScanner is the interface that adds the UnreadRune method to the
  801. basic ReadRune method.
  802. </p>
  803. <p>
  804. UnreadRune causes the next call to ReadRune to return the same rune
  805. as the previous call to ReadRune.
  806. It may be an error to call UnreadRune twice without an intervening
  807. call to ReadRune.
  808. </p>
  809. <h2 id="SectionReader">type <a href="http://localhost:6060/src/io/io.go?s=14634:14718#L429">SectionReader</a></h2>
  810. <pre>type SectionReader struct {
  811. <span class="comment">// contains filtered or unexported fields</span>
  812. }</pre>
  813. <p>
  814. SectionReader implements Read, Seek, and ReadAt on a section
  815. of an underlying ReaderAt.
  816. </p>
  817. <div id="example_SectionReader" class="toggle">
  818. <div class="collapsed">
  819. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  820. </div>
  821. <div class="expanded">
  822. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  823. <p>Code:</p>
  824. <pre class="code">r := strings.NewReader(&#34;some io.Reader stream to be read\n&#34;)
  825. s := io.NewSectionReader(r, 5, 17)
  826. if _, err := io.Copy(os.Stdout, s); err != nil {
  827. log.Fatal(err)
  828. }
  829. <span class="comment"></pre>
  830. <p>Output:</p>
  831. <pre class="output">io.Reader stream
  832. </pre>
  833. </div>
  834. </div>
  835. <h3 id="NewSectionReader">func <a href="http://localhost:6060/src/io/io.go?s=14421:14489#L423">NewSectionReader</a></h3>
  836. <pre>func NewSectionReader(r <a href="index.html#ReaderAt">ReaderAt</a>, off <a href="../builtin/index.html#int64">int64</a>, n <a href="../builtin/index.html#int64">int64</a>) *<a href="index.html#SectionReader">SectionReader</a></pre>
  837. <p>
  838. NewSectionReader returns a SectionReader that reads from r
  839. starting at offset off and stops with EOF after n bytes.
  840. </p>
  841. <h3 id="SectionReader.Read">func (*SectionReader) <a href="http://localhost:6060/src/io/io.go?s=14720:14777#L436">Read</a></h3>
  842. <pre>func (s *<a href="index.html#SectionReader">SectionReader</a>) Read(p []<a href="../builtin/index.html#byte">byte</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  843. <h3 id="SectionReader.ReadAt">func (*SectionReader) <a href="http://localhost:6060/src/io/io.go?s=15357:15427#L469">ReadAt</a></h3>
  844. <pre>func (s *<a href="index.html#SectionReader">SectionReader</a>) ReadAt(p []<a href="../builtin/index.html#byte">byte</a>, off <a href="../builtin/index.html#int64">int64</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
  845. <div id="example_SectionReader_ReadAt" class="toggle">
  846. <div class="collapsed">
  847. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  848. </div>
  849. <div class="expanded">
  850. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  851. <p>Code:</p>
  852. <pre class="code">r := strings.NewReader(&#34;some io.Reader stream to be read\n&#34;)
  853. s := io.NewSectionReader(r, 5, 16)
  854. buf := make([]byte, 6)
  855. if _, err := s.ReadAt(buf, 10); err != nil {
  856. log.Fatal(err)
  857. }
  858. fmt.Printf(&#34;%s\n&#34;, buf)
  859. <span class="comment"></pre>
  860. <p>Output:</p>
  861. <pre class="output">stream
  862. </pre>
  863. </div>
  864. </div>
  865. <h3 id="SectionReader.Seek">func (*SectionReader) <a href="http://localhost:6060/src/io/io.go?s=15054:15123#L451">Seek</a></h3>
  866. <pre>func (s *<a href="index.html#SectionReader">SectionReader</a>) Seek(offset <a href="../builtin/index.html#int64">int64</a>, whence <a href="../builtin/index.html#int">int</a>) (<a href="../builtin/index.html#int64">int64</a>, <a href="../builtin/index.html#error">error</a>)</pre>
  867. <div id="example_SectionReader_Seek" class="toggle">
  868. <div class="collapsed">
  869. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  870. </div>
  871. <div class="expanded">
  872. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  873. <p>Code:</p>
  874. <pre class="code">r := strings.NewReader(&#34;some io.Reader stream to be read\n&#34;)
  875. s := io.NewSectionReader(r, 5, 16)
  876. if _, err := s.Seek(10, 0); err != nil {
  877. log.Fatal(err)
  878. }
  879. buf := make([]byte, 6)
  880. if _, err := s.Read(buf); err != nil {
  881. log.Fatal(err)
  882. }
  883. fmt.Printf(&#34;%s\n&#34;, buf)
  884. <span class="comment"></pre>
  885. <p>Output:</p>
  886. <pre class="output">stream
  887. </pre>
  888. </div>
  889. </div>
  890. <h3 id="SectionReader.Size">func (*SectionReader) <a href="http://localhost:6060/src/io/io.go?s=15730:15766#L486">Size</a></h3>
  891. <pre>func (s *<a href="index.html#SectionReader">SectionReader</a>) Size() <a href="../builtin/index.html#int64">int64</a></pre>
  892. <p>
  893. Size returns the size of the section in bytes.
  894. </p>
  895. <h2 id="Seeker">type <a href="http://localhost:6060/src/io/io.go?s=4482:4554#L96">Seeker</a></h2>
  896. <pre>type Seeker interface {
  897. Seek(offset <a href="../builtin/index.html#int64">int64</a>, whence <a href="../builtin/index.html#int">int</a>) (<a href="../builtin/index.html#int64">int64</a>, <a href="../builtin/index.html#error">error</a>)
  898. }</pre>
  899. <p>
  900. Seeker is the interface that wraps the basic Seek method.
  901. </p>
  902. <p>
  903. Seek sets the offset for the next Read or Write to offset,
  904. interpreted according to whence: 0 means relative to the start of
  905. the file, 1 means relative to the current offset, and 2 means
  906. relative to the end. Seek returns the new offset relative to the
  907. start of the file and an error, if any.
  908. </p>
  909. <p>
  910. Seeking to an offset before the start of the file is an error.
  911. Seeking to any positive offset is legal, but the behavior of subsequent
  912. I/O operations on the underlying object is implementation-dependent.
  913. </p>
  914. <h2 id="WriteCloser">type <a href="http://localhost:6060/src/io/io.go?s=4883:4929#L113">WriteCloser</a></h2>
  915. <pre>type WriteCloser interface {
  916. <a href="index.html#Writer">Writer</a>
  917. <a href="index.html#Closer">Closer</a>
  918. }</pre>
  919. <p>
  920. WriteCloser is the interface that groups the basic Write and Close methods.
  921. </p>
  922. <h2 id="WriteSeeker">type <a href="http://localhost:6060/src/io/io.go?s=5281:5327#L132">WriteSeeker</a></h2>
  923. <pre>type WriteSeeker interface {
  924. <a href="index.html#Writer">Writer</a>
  925. <a href="index.html#Seeker">Seeker</a>
  926. }</pre>
  927. <p>
  928. WriteSeeker is the interface that groups the basic Write and Seek methods.
  929. </p>
  930. <h2 id="Writer">type <a href="http://localhost:6060/src/io/io.go?s=3604:3665#L73">Writer</a></h2>
  931. <pre>type Writer interface {
  932. Write(p []<a href="../builtin/index.html#byte">byte</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)
  933. }</pre>
  934. <p>
  935. Writer is the interface that wraps the basic Write method.
  936. </p>
  937. <p>
  938. Write writes len(p) bytes from p to the underlying data stream.
  939. It returns the number of bytes written from p (0 &lt;= n &lt;= len(p))
  940. and any error encountered that caused the write to stop early.
  941. Write must return a non-nil error if it returns n &lt; len(p).
  942. Write must not modify the slice data, even temporarily.
  943. </p>
  944. <p>
  945. Implementations must not retain p.
  946. </p>
  947. <h3 id="MultiWriter">func <a href="http://localhost:6060/src/io/multi.go?s=1804:1846#L71">MultiWriter</a></h3>
  948. <pre>func MultiWriter(writers ...<a href="index.html#Writer">Writer</a>) <a href="index.html#Writer">Writer</a></pre>
  949. <p>
  950. MultiWriter creates a writer that duplicates its writes to all the
  951. provided writers, similar to the Unix tee(1) command.
  952. </p>
  953. <div id="example_MultiWriter" class="toggle">
  954. <div class="collapsed">
  955. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  956. </div>
  957. <div class="expanded">
  958. <p class="exampleHeading toggleButton"><span class="text">Example</span></p>
  959. <p>Code:</p>
  960. <pre class="code">r := strings.NewReader(&#34;some io.Reader stream to be read\n&#34;)
  961. var buf1, buf2 bytes.Buffer
  962. w := io.MultiWriter(&amp;buf1, &amp;buf2)
  963. if _, err := io.Copy(w, r); err != nil {
  964. log.Fatal(err)
  965. }
  966. fmt.Print(buf1.String())
  967. fmt.Print(buf2.String())
  968. <span class="comment"></pre>
  969. <p>Output:</p>
  970. <pre class="output">some io.Reader stream to be read
  971. some io.Reader stream to be read
  972. </pre>
  973. </div>
  974. </div>
  975. <h2 id="WriterAt">type <a href="http://localhost:6060/src/io/io.go?s=8059:8135#L211">WriterAt</a></h2>
  976. <pre>type WriterAt interface {
  977. WriteAt(p []<a href="../builtin/index.html#byte">byte</a>, off <a href="../builtin/index.html#int64">int64</a>) (n <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)
  978. }</pre>
  979. <p>
  980. WriterAt is the interface that wraps the basic WriteAt method.
  981. </p>
  982. <p>
  983. WriteAt writes len(p) bytes from p to the underlying data stream
  984. at offset off. It returns the number of bytes written from p (0 &lt;= n &lt;= len(p))
  985. and any error encountered that caused the write to stop early.
  986. WriteAt must return a non-nil error if it returns n &lt; len(p).
  987. </p>
  988. <p>
  989. If WriteAt is writing to a destination with a seek offset,
  990. WriteAt should not affect nor be affected by the underlying
  991. seek offset.
  992. </p>
  993. <p>
  994. Clients of WriteAt can execute parallel WriteAt calls on the same
  995. destination if the ranges do not overlap.
  996. </p>
  997. <p>
  998. Implementations must not retain p.
  999. </p>
  1000. <h2 id="WriterTo">type <a href="http://localhost:6060/src/io/io.go?s=6161:6228#L162">WriterTo</a></h2>
  1001. <pre>type WriterTo interface {
  1002. WriteTo(w <a href="index.html#Writer">Writer</a>) (n <a href="../builtin/index.html#int64">int64</a>, err <a href="../builtin/index.html#error">error</a>)
  1003. }</pre>
  1004. <p>
  1005. WriterTo is the interface that wraps the WriteTo method.
  1006. </p>
  1007. <p>
  1008. WriteTo writes data to w until there&#39;s no more data to write or
  1009. when an error occurs. The return value n is the number of bytes
  1010. written. Any error encountered during the write is also returned.
  1011. </p>
  1012. <p>
  1013. The Copy function uses WriterTo if available.
  1014. </p>
  1015. <h2 id="pkg-subdirectories">Subdirectories</h2>
  1016. <div class="pkg-dir">
  1017. <table>
  1018. <tr>
  1019. <th class="pkg-name">Name</th>
  1020. <th class="pkg-synopsis">Synopsis</th>
  1021. </tr>
  1022. <tr>
  1023. <td colspan="2"><a href="http://localhost:6060/pkg/">..</a></td>
  1024. </tr>
  1025. <tr>
  1026. <td class="pkg-name" style="padding-left: 0px;">
  1027. <a href="ioutil/index.html">ioutil</a>
  1028. </td>
  1029. <td class="pkg-synopsis">
  1030. Package ioutil implements some I/O utility functions.
  1031. </td>
  1032. </tr>
  1033. </table>
  1034. </div>
  1035. <div id="footer">
  1036. Build version go1.6.<br>
  1037. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  1038. the content of this page is licensed under the
  1039. Creative Commons Attribution 3.0 License,
  1040. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  1041. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  1042. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  1043. </div>
  1044. </div><!-- .container -->
  1045. </div><!-- #page -->
  1046. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  1047. <script type="text/javascript" src="../../lib/godoc/jquery.js"></script>
  1048. <script type="text/javascript" src="../../lib/godoc/jquery.treeview.js"></script>
  1049. <script type="text/javascript" src="../../lib/godoc/jquery.treeview.edit.js"></script>
  1050. <script type="text/javascript" src="../../lib/godoc/godocs.js"></script>
  1051. </body>
  1052. </html>