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.
 
 
 

1276 lines
45 KiB

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#375EAB">
<title>bufio - The Go Programming Language</title>
<link type="text/css" rel="stylesheet" href="../../lib/godoc/style.css">
<link rel="stylesheet" href="../../lib/godoc/jquery.treeview.css">
<script type="text/javascript">window.initFuncs = [];</script>
</head>
<body>
<div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
...
</div><!-- #lowframe -->
<div id="topbar" class="wide"><div class="container">
<div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
<div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
<a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
<form method="GET" action="http://localhost:6060/search">
<div id="menu">
<a href="http://localhost:6060/doc/">Documents</a>
<a href="http://localhost:6060/pkg/">Packages</a>
<a href="http://localhost:6060/project/">The Project</a>
<a href="http://localhost:6060/help/">Help</a>
<a href="http://localhost:6060/blog/">Blog</a>
<input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
</div>
</form>
</div></div>
<div id="page" class="wide">
<div class="container">
<h1>Package bufio</h1>
<div id="nav"></div>
<!--
Copyright 2009 The Go Authors. All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.
-->
<!--
Note: Static (i.e., not template-generated) href and id
attributes start with "pkg-" to make it impossible for
them to conflict with generated attributes (some of which
correspond to Go identifiers).
-->
<script type='text/javascript'>
document.ANALYSIS_DATA = null;
document.CALLGRAPH = null;
</script>
<div id="short-nav">
<dl>
<dd><code>import "bufio"</code></dd>
</dl>
<dl>
<dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
<dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
<dd><a href="index.html#pkg-examples" class="examplesLink">Examples</a></dd>
</dl>
</div>
<!-- The package's Name is printed as title by the top-level template -->
<div id="pkg-overview" class="toggleVisible">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
</div>
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
<p>
Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer
object, creating another object (Reader or Writer) that also implements
the interface but provides buffering and some help for textual I/O.
</p>
</div>
</div>
<div id="pkg-index" class="toggleVisible">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
</div>
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
<!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
<div id="manual-nav">
<dl>
<dd><a href="index.html#pkg-constants">Constants</a></dd>
<dd><a href="index.html#pkg-variables">Variables</a></dd>
<dd><a href="index.html#ScanBytes">func ScanBytes(data []byte, atEOF bool) (advance int, token []byte, err error)</a></dd>
<dd><a href="index.html#ScanLines">func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error)</a></dd>
<dd><a href="index.html#ScanRunes">func ScanRunes(data []byte, atEOF bool) (advance int, token []byte, err error)</a></dd>
<dd><a href="index.html#ScanWords">func ScanWords(data []byte, atEOF bool) (advance int, token []byte, err error)</a></dd>
<dd><a href="index.html#ReadWriter">type ReadWriter</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#NewReadWriter">func NewReadWriter(r *Reader, w *Writer) *ReadWriter</a></dd>
<dd><a href="index.html#Reader">type Reader</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#NewReader">func NewReader(rd io.Reader) *Reader</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#NewReaderSize">func NewReaderSize(rd io.Reader, size int) *Reader</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.Buffered">func (b *Reader) Buffered() int</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.Discard">func (b *Reader) Discard(n int) (discarded int, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.Peek">func (b *Reader) Peek(n int) ([]byte, error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.Read">func (b *Reader) Read(p []byte) (n int, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.ReadByte">func (b *Reader) ReadByte() (c byte, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.ReadBytes">func (b *Reader) ReadBytes(delim byte) (line []byte, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.ReadLine">func (b *Reader) ReadLine() (line []byte, isPrefix bool, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.ReadRune">func (b *Reader) ReadRune() (r rune, size int, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.ReadSlice">func (b *Reader) ReadSlice(delim byte) (line []byte, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.ReadString">func (b *Reader) ReadString(delim byte) (line string, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.Reset">func (b *Reader) Reset(r io.Reader)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.UnreadByte">func (b *Reader) UnreadByte() error</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.UnreadRune">func (b *Reader) UnreadRune() error</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Reader.WriteTo">func (b *Reader) WriteTo(w io.Writer) (n int64, err error)</a></dd>
<dd><a href="index.html#Scanner">type Scanner</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#NewScanner">func NewScanner(r io.Reader) *Scanner</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Scanner.Buffer">func (s *Scanner) Buffer(buf []byte, max int)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Scanner.Bytes">func (s *Scanner) Bytes() []byte</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Scanner.Err">func (s *Scanner) Err() error</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Scanner.Scan">func (s *Scanner) Scan() bool</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Scanner.Split">func (s *Scanner) Split(split SplitFunc)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Scanner.Text">func (s *Scanner) Text() string</a></dd>
<dd><a href="index.html#SplitFunc">type SplitFunc</a></dd>
<dd><a href="index.html#Writer">type Writer</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#NewWriter">func NewWriter(w io.Writer) *Writer</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#NewWriterSize">func NewWriterSize(w io.Writer, size int) *Writer</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Writer.Available">func (b *Writer) Available() int</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Writer.Buffered">func (b *Writer) Buffered() int</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Writer.Flush">func (b *Writer) Flush() error</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Writer.ReadFrom">func (b *Writer) ReadFrom(r io.Reader) (n int64, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Writer.Reset">func (b *Writer) Reset(w io.Writer)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Writer.Write">func (b *Writer) Write(p []byte) (nn int, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Writer.WriteByte">func (b *Writer) WriteByte(c byte) error</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Writer.WriteRune">func (b *Writer) WriteRune(r rune) (size int, err error)</a></dd>
<dd>&nbsp; &nbsp; <a href="index.html#Writer.WriteString">func (b *Writer) WriteString(s string) (int, error)</a></dd>
</dl>
</div><!-- #manual-nav -->
<div id="pkg-examples">
<h4>Examples</h4>
<dl>
<dd><a class="exampleLink" href="index.html#example_Scanner_custom">Scanner (Custom)</a></dd>
<dd><a class="exampleLink" href="index.html#example_Scanner_emptyFinalToken">Scanner (EmptyFinalToken)</a></dd>
<dd><a class="exampleLink" href="index.html#example_Scanner_lines">Scanner (Lines)</a></dd>
<dd><a class="exampleLink" href="index.html#example_Scanner_words">Scanner (Words)</a></dd>
<dd><a class="exampleLink" href="index.html#example_Writer">Writer</a></dd>
</dl>
</div>
<h4>Package files</h4>
<p>
<span style="font-size:90%">
<a href="http://localhost:6060/src/bufio/bufio.go">bufio.go</a>
<a href="http://localhost:6060/src/bufio/scan.go">scan.go</a>
</span>
</p>
</div><!-- .expanded -->
</div><!-- #pkg-index -->
<div id="pkg-callgraph" class="toggle" style="display: none">
<div class="collapsed">
<h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
</div> <!-- .expanded -->
<div class="expanded">
<h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
<p>
In the call graph viewer below, each node
is a function belonging to this package
and its children are the functions it
calls&mdash;perhaps dynamically.
</p>
<p>
The root nodes are the entry points of the
package: functions that may be called from
outside the package.
There may be non-exported or anonymous
functions among them if they are called
dynamically from another package.
</p>
<p>
Click a node to visit that function's source code.
From there you can visit its callers by
clicking its declaring <code>func</code>
token.
</p>
<p>
Functions may be omitted if they were
determined to be unreachable in the
particular programs or tests that were
analyzed.
</p>
<!-- Zero means show all package entry points. -->
<ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
</div>
</div> <!-- #pkg-callgraph -->
<h2 id="pkg-constants">Constants</h2>
<pre>const (
<span class="comment">// MaxScanTokenSize is the maximum size used to buffer a token</span>
<span class="comment">// unless the user provides an explicit buffer with Scan.Buffer.</span>
<span class="comment">// The actual maximum token size may be smaller as the buffer</span>
<span class="comment">// may need to include, for instance, a newline.</span>
<span id="MaxScanTokenSize">MaxScanTokenSize</span> = 64 * 1024
)</pre>
<h2 id="pkg-variables">Variables</h2>
<pre>var (
<span id="ErrInvalidUnreadByte">ErrInvalidUnreadByte</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;bufio: invalid use of UnreadByte&#34;)
<span id="ErrInvalidUnreadRune">ErrInvalidUnreadRune</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;bufio: invalid use of UnreadRune&#34;)
<span id="ErrBufferFull">ErrBufferFull</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;bufio: buffer full&#34;)
<span id="ErrNegativeCount">ErrNegativeCount</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;bufio: negative count&#34;)
)</pre>
<pre>var (
<span id="ErrTooLong">ErrTooLong</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;bufio.Scanner: token too long&#34;)
<span id="ErrNegativeAdvance">ErrNegativeAdvance</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;bufio.Scanner: SplitFunc returns negative advance count&#34;)
<span id="ErrAdvanceTooFar">ErrAdvanceTooFar</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;bufio.Scanner: SplitFunc returns advance count beyond input&#34;)
)</pre>
<p>
Errors returned by Scanner.
</p>
<pre>var <span id="ErrFinalToken">ErrFinalToken</span> = <a href="../errors/index.html">errors</a>.<a href="../errors/index.html#New">New</a>(&#34;final token&#34;)</pre>
<p>
ErrFinalToken is a special sentinel error value. It is intended to be
returned by a Split function to indicate that the token being delivered
with the error is the last token and scanning should stop after this one.
After ErrFinalToken is received by Scan, scanning stops with no error.
The value is useful to stop processing early or when it is necessary to
deliver a final empty token. One could achieve the same behavior
with a custom error value but providing one here is tidier.
See the emptyFinalToken example for a use of this value.
</p>
<h2 id="ScanBytes">func <a href="http://localhost:6060/src/bufio/scan.go?s=9294:9372#L269">ScanBytes</a></h2>
<pre>func ScanBytes(data []<a href="../builtin/index.html#byte">byte</a>, atEOF <a href="../builtin/index.html#bool">bool</a>) (advance <a href="../builtin/index.html#int">int</a>, token []<a href="../builtin/index.html#byte">byte</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
ScanBytes is a split function for a Scanner that returns each byte as a token.
</p>
<h2 id="ScanLines">func <a href="http://localhost:6060/src/bufio/scan.go?s=11500:11578#L330">ScanLines</a></h2>
<pre>func ScanLines(data []<a href="../builtin/index.html#byte">byte</a>, atEOF <a href="../builtin/index.html#bool">bool</a>) (advance <a href="../builtin/index.html#int">int</a>, token []<a href="../builtin/index.html#byte">byte</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
ScanLines is a split function for a Scanner that returns each line of
text, stripped of any trailing end-of-line marker. The returned line may
be empty. The end-of-line marker is one optional carriage return followed
by one mandatory newline. In regular expression notation, it is `\r?\n`.
The last non-empty line of input will be returned even if it has no
newline.
</p>
<h2 id="ScanRunes">func <a href="http://localhost:6060/src/bufio/scan.go?s=9941:10019#L284">ScanRunes</a></h2>
<pre>func ScanRunes(data []<a href="../builtin/index.html#byte">byte</a>, atEOF <a href="../builtin/index.html#bool">bool</a>) (advance <a href="../builtin/index.html#int">int</a>, token []<a href="../builtin/index.html#byte">byte</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
ScanRunes is a split function for a Scanner that returns each
UTF-8-encoded rune as a token. The sequence of runes returned is
equivalent to that from a range loop over the input as a string, which
means that erroneous UTF-8 encodings translate to U+FFFD = &#34;\xef\xbf\xbd&#34;.
Because of the Scan interface, this makes it impossible for the client to
distinguish correctly encoded replacement runes from encoding errors.
</p>
<h2 id="ScanWords">func <a href="http://localhost:6060/src/bufio/scan.go?s=12794:12872#L375">ScanWords</a></h2>
<pre>func ScanWords(data []<a href="../builtin/index.html#byte">byte</a>, atEOF <a href="../builtin/index.html#bool">bool</a>) (advance <a href="../builtin/index.html#int">int</a>, token []<a href="../builtin/index.html#byte">byte</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
ScanWords is a split function for a Scanner that returns each
space-separated word of text, with surrounding spaces deleted. It will
never return an empty string. The definition of space is set by
unicode.IsSpace.
</p>
<h2 id="ReadWriter">type <a href="http://localhost:6060/src/bufio/bufio.go?s=17449:17493#L712">ReadWriter</a></h2>
<pre>type ReadWriter struct {
*<a href="index.html#Reader">Reader</a>
*<a href="index.html#Writer">Writer</a>
}</pre>
<p>
ReadWriter stores pointers to a Reader and a Writer.
It implements io.ReadWriter.
</p>
<h3 id="NewReadWriter">func <a href="http://localhost:6060/src/bufio/bufio.go?s=17567:17619#L718">NewReadWriter</a></h3>
<pre>func NewReadWriter(r *<a href="index.html#Reader">Reader</a>, w *<a href="index.html#Writer">Writer</a>) *<a href="index.html#ReadWriter">ReadWriter</a></pre>
<p>
NewReadWriter allocates a new ReadWriter that dispatches to r and w.
</p>
<h2 id="Reader">type <a href="http://localhost:6060/src/bufio/bufio.go?s=830:1042#L21">Reader</a></h2>
<pre>type Reader struct {
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
Reader implements buffering for an io.Reader object.
</p>
<h3 id="NewReader">func <a href="http://localhost:6060/src/bufio/bufio.go?s=1648:1684#L51">NewReader</a></h3>
<pre>func NewReader(rd <a href="../io/index.html">io</a>.<a href="../io/index.html#Reader">Reader</a>) *<a href="index.html#Reader">Reader</a></pre>
<p>
NewReader returns a new Reader whose buffer has the default size.
</p>
<h3 id="NewReaderSize">func <a href="http://localhost:6060/src/bufio/bufio.go?s=1305:1355#L36">NewReaderSize</a></h3>
<pre>func NewReaderSize(rd <a href="../io/index.html">io</a>.<a href="../io/index.html#Reader">Reader</a>, size <a href="../builtin/index.html#int">int</a>) *<a href="index.html#Reader">Reader</a></pre>
<p>
NewReaderSize returns a new Reader whose buffer has at least the specified
size. If the argument io.Reader is already a Reader with large enough
size, it returns the underlying Reader.
</p>
<h3 id="Reader.Buffered">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=7000:7031#L281">Buffered</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) Buffered() <a href="../builtin/index.html#int">int</a></pre>
<p>
Buffered returns the number of bytes that can be read from the current buffer.
</p>
<h3 id="Reader.Discard">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=3757:3815#L142">Discard</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) Discard(n <a href="../builtin/index.html#int">int</a>) (discarded <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
Discard skips the next n bytes, returning the number of bytes discarded.
</p>
<p>
If Discard skips fewer than n bytes, it also returns an error.
If 0 &lt;= n &lt;= b.Buffered(), Discard is guaranteed to succeed without
reading from the underlying io.Reader.
</p>
<h3 id="Reader.Peek">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=3036:3080#L113">Peek</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) Peek(n <a href="../builtin/index.html#int">int</a>) ([]<a href="../builtin/index.html#byte">byte</a>, <a href="../builtin/index.html#error">error</a>)</pre>
<p>
Peek returns the next n bytes without advancing the reader. The bytes stop
being valid at the next read call. If Peek returns fewer than n bytes, it
also returns an error explaining why the read is short. The error is
ErrBufferFull if n is larger than b&#39;s buffer size.
</p>
<h3 id="Reader.Read">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=4402:4452#L175">Read</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</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>
<p>
Read reads data into p.
It returns the number of bytes read into p.
The bytes are taken from at most one Read on the underlying Reader,
hence n may be less than len(p).
At EOF, the count will be zero and err will be io.EOF.
</p>
<h3 id="Reader.ReadByte">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=5158:5205#L213">ReadByte</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) ReadByte() (c <a href="../builtin/index.html#byte">byte</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
ReadByte reads and returns a single byte.
If no byte is available, returns an error.
</p>
<h3 id="Reader.ReadBytes">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=10455:10518#L388">ReadBytes</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) ReadBytes(delim <a href="../builtin/index.html#byte">byte</a>) (line []<a href="../builtin/index.html#byte">byte</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
ReadBytes reads until the first occurrence of delim in the input,
returning a slice containing the data up to and including the delimiter.
If ReadBytes encounters an error before finding a delimiter,
it returns the data read before the error and the error itself (often io.EOF).
ReadBytes returns err != nil if and only if the returned data does not end in
delim.
For simple uses, a Scanner may be more convenient.
</p>
<h3 id="Reader.ReadLine">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=9262:9329#L346">ReadLine</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) ReadLine() (line []<a href="../builtin/index.html#byte">byte</a>, isPrefix <a href="../builtin/index.html#bool">bool</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
ReadLine is a low-level line-reading primitive. Most callers should use
ReadBytes(&#39;\n&#39;) or ReadString(&#39;\n&#39;) instead or use a Scanner.
</p>
<p>
ReadLine tries to return a single line, not including the end-of-line bytes.
If the line was too long for the buffer then isPrefix is set and the
beginning of the line is returned. The rest of the line will be returned
from future calls. isPrefix will be false when returning the last fragment
of the line. The returned buffer is only valid until the next call to
ReadLine. ReadLine either returns a non-nil line or it returns an error,
never both.
</p>
<p>
The text returned from ReadLine does not include the line end (&#34;\r\n&#34; or &#34;\n&#34;).
No indication or error is given if the input ends without a final line end.
Calling UnreadByte after ReadLine will always unread the last byte read
(possibly a character belonging to the line end) even if that byte is not
part of the line returned by ReadLine.
</p>
<h3 id="Reader.ReadRune">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=5996:6053#L248">ReadRune</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) 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>)</pre>
<p>
ReadRune reads a single UTF-8 encoded Unicode character and returns the
rune and its size in bytes. If the encoded rune is invalid, it consumes one byte
and returns unicode.ReplacementChar (U+FFFD) with a size of 1.
</p>
<h3 id="Reader.ReadSlice">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=7681:7744#L293">ReadSlice</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) ReadSlice(delim <a href="../builtin/index.html#byte">byte</a>) (line []<a href="../builtin/index.html#byte">byte</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
ReadSlice reads until the first occurrence of delim in the input,
returning a slice pointing at the bytes in the buffer.
The bytes stop being valid at the next read.
If ReadSlice encounters an error before finding a delimiter,
it returns all the data in the buffer and the error itself (often io.EOF).
ReadSlice fails with error ErrBufferFull if the buffer fills without a delim.
Because the data returned from ReadSlice will be overwritten
by the next I/O operation, most clients should use
ReadBytes or ReadString instead.
ReadSlice returns err != nil if and only if line does not end in delim.
</p>
<h3 id="Reader.ReadString">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=11657:11721#L435">ReadString</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) ReadString(delim <a href="../builtin/index.html#byte">byte</a>) (line <a href="../builtin/index.html#string">string</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
ReadString reads until the first occurrence of delim in the input,
returning a string containing the data up to and including the delimiter.
If ReadString encounters an error before finding a delimiter,
it returns the data read before the error and the error itself (often io.EOF).
ReadString returns err != nil if and only if the returned data does not end in
delim.
For simple uses, a Scanner may be more convenient.
</p>
<h3 id="Reader.Reset">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=1839:1874#L57">Reset</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) Reset(r <a href="../io/index.html">io</a>.<a href="../io/index.html#Reader">Reader</a>)</pre>
<p>
Reset discards any buffered data, resets all state, and switches
the buffered reader to read from r.
</p>
<h3 id="Reader.UnreadByte">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=5477:5512#L228">UnreadByte</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) UnreadByte() <a href="../builtin/index.html#error">error</a></pre>
<p>
UnreadByte unreads the last byte. Only the most recently read byte can be unread.
</p>
<h3 id="Reader.UnreadRune">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=6722:6757#L270">UnreadRune</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) UnreadRune() <a href="../builtin/index.html#error">error</a></pre>
<p>
UnreadRune unreads the last rune. If the most recent read operation on
the buffer was not a ReadRune, UnreadRune returns an error. (In this
regard it is stricter than UnreadByte, which will unread the last byte
from any read operation.)
</p>
<h3 id="Reader.WriteTo">func (*Reader) <a href="http://localhost:6060/src/bufio/bufio.go?s=11836:11894#L442">WriteTo</a></h3>
<pre>func (b *<a href="index.html#Reader">Reader</a>) WriteTo(w <a href="../io/index.html">io</a>.<a href="../io/index.html#Writer">Writer</a>) (n <a href="../builtin/index.html#int64">int64</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
WriteTo implements io.WriterTo.
</p>
<h2 id="Scanner">type <a href="http://localhost:6060/src/bufio/scan.go?s=1184:1841#L20">Scanner</a></h2>
<pre>type Scanner struct {
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
Scanner provides a convenient interface for reading data such as
a file of newline-delimited lines of text. Successive calls to
the Scan method will step through the &#39;tokens&#39; of a file, skipping
the bytes between the tokens. The specification of a token is
defined by a split function of type SplitFunc; the default split
function breaks the input into lines with line termination stripped. Split
functions are defined in this package for scanning a file into
lines, bytes, UTF-8-encoded runes, and space-delimited words. The
client may instead provide a custom split function.
</p>
<p>
Scanning stops unrecoverably at EOF, the first I/O error, or a token too
large to fit in the buffer. When a scan stops, the reader may have
advanced arbitrarily far past the last token. Programs that need more
control over error handling or large tokens, or must run sequential scans
on a reader, should use bufio.Reader instead.
</p>
<div id="example_Scanner_custom" class="toggle">
<div class="collapsed">
<p class="exampleHeading toggleButton"><span class="text">Example (Custom)</span></p>
</div>
<div class="expanded">
<p class="exampleHeading toggleButton"><span class="text">Example (Custom)</span></p>
<p>Use a Scanner with a custom split function (built by wrapping ScanWords) to validate
32-bit decimal input.
</p>
<p>Code:</p>
<pre class="code"><span class="comment">// An artificial input source.</span>
const input = &#34;1234 5678 1234567901234567890&#34;
scanner := bufio.NewScanner(strings.NewReader(input))
<span class="comment">// Create a custom split function by wrapping the existing ScanWords function.</span>
split := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
advance, token, err = bufio.ScanWords(data, atEOF)
if err == nil &amp;&amp; token != nil {
_, err = strconv.ParseInt(string(token), 10, 32)
}
return
}
<span class="comment">// Set the split function for the scanning operation.</span>
scanner.Split(split)
<span class="comment">// Validate the input</span>
for scanner.Scan() {
fmt.Printf(&#34;%s\n&#34;, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Printf(&#34;Invalid input: %s&#34;, err)
}
<span class="comment"></pre>
<p>Output:</p>
<pre class="output">1234
5678
Invalid input: strconv.ParseInt: parsing &#34;1234567901234567890&#34;: value out of range
</pre>
</div>
</div>
<div id="example_Scanner_emptyFinalToken" class="toggle">
<div class="collapsed">
<p class="exampleHeading toggleButton"><span class="text">Example (EmptyFinalToken)</span></p>
</div>
<div class="expanded">
<p class="exampleHeading toggleButton"><span class="text">Example (EmptyFinalToken)</span></p>
<p>Use a Scanner with a custom split function to parse a comma-separated
list with an empty final value.
</p>
<p>Code:</p>
<pre class="code"><span class="comment">// Comma-separated list; last entry is empty.</span>
const input = &#34;1,2,3,4,&#34;
scanner := bufio.NewScanner(strings.NewReader(input))
<span class="comment">// Define a split function that separates on commas.</span>
onComma := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
for i := 0; i &lt; len(data); i++ {
if data[i] == &#39;,&#39; {
return i + 1, data[:i], nil
}
}
<span class="comment">// There is one final token to be delivered, which may be the empty string.</span>
<span class="comment">// Returning bufio.ErrFinalToken here tells Scan there are no more tokens after this</span>
<span class="comment">// but does not trigger an error to be returned from Scan itself.</span>
return 0, data, bufio.ErrFinalToken
}
scanner.Split(onComma)
<span class="comment">// Scan.</span>
for scanner.Scan() {
fmt.Printf(&#34;%q &#34;, scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, &#34;reading input:&#34;, err)
}
<span class="comment"></pre>
<p>Output:</p>
<pre class="output">&#34;1&#34; &#34;2&#34; &#34;3&#34; &#34;4&#34; &#34;&#34;
</pre>
</div>
</div>
<div id="example_Scanner_lines" class="toggle">
<div class="collapsed">
<p class="exampleHeading toggleButton"><span class="text">Example (Lines)</span></p>
</div>
<div class="expanded">
<p class="exampleHeading toggleButton"><span class="text">Example (Lines)</span></p>
<p>The simplest use of a Scanner, to read standard input as a set of lines.
</p>
<p>Code:</p>
<pre class="code">
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Println(scanner.Text()) <span class="comment">// Println will add back the final &#39;\n&#39;</span>
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, &#34;reading standard input:&#34;, err)
}
</pre>
</div>
</div>
<div id="example_Scanner_words" class="toggle">
<div class="collapsed">
<p class="exampleHeading toggleButton"><span class="text">Example (Words)</span></p>
</div>
<div class="expanded">
<p class="exampleHeading toggleButton"><span class="text">Example (Words)</span></p>
<p>Use a Scanner to implement a simple word-count utility by scanning the
input as a sequence of space-delimited tokens.
</p>
<p>Code:</p>
<pre class="code"><span class="comment">// An artificial input source.</span>
const input = &#34;Now is the winter of our discontent,\nMade glorious summer by this sun of York.\n&#34;
scanner := bufio.NewScanner(strings.NewReader(input))
<span class="comment">// Set the split function for the scanning operation.</span>
scanner.Split(bufio.ScanWords)
<span class="comment">// Count the words.</span>
count := 0
for scanner.Scan() {
count++
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, &#34;reading input:&#34;, err)
}
fmt.Printf(&#34;%d\n&#34;, count)
<span class="comment"></pre>
<p>Output:</p>
<pre class="output">15
</pre>
</div>
</div>
<h3 id="NewScanner">func <a href="http://localhost:6060/src/bufio/scan.go?s=3594:3631#L71">NewScanner</a></h3>
<pre>func NewScanner(r <a href="../io/index.html">io</a>.<a href="../io/index.html#Reader">Reader</a>) *<a href="index.html#Scanner">Scanner</a></pre>
<p>
NewScanner returns a new Scanner to read from r.
The split function defaults to ScanLines.
</p>
<h3 id="Scanner.Buffer">func (*Scanner) <a href="http://localhost:6060/src/bufio/scan.go?s=8759:8804#L247">Buffer</a></h3>
<pre>func (s *<a href="index.html#Scanner">Scanner</a>) Buffer(buf []<a href="../builtin/index.html#byte">byte</a>, max <a href="../builtin/index.html#int">int</a>)</pre>
<p>
Buffer sets the initial buffer to use when scanning and the maximum
size of buffer that may be allocated during scanning. The maximum
token size is the larger of max and cap(buf). If max &lt;= cap(buf),
Scan will use this buffer only and do no allocation.
</p>
<p>
By default, Scan uses an internal buffer and sets the
maximum token size to MaxScanTokenSize.
</p>
<p>
Buffer panics if it is called after scanning has started.
</p>
<h3 id="Scanner.Bytes">func (*Scanner) <a href="http://localhost:6060/src/bufio/scan.go?s=4092:4124#L90">Bytes</a></h3>
<pre>func (s *<a href="index.html#Scanner">Scanner</a>) Bytes() []<a href="../builtin/index.html#byte">byte</a></pre>
<p>
Bytes returns the most recent token generated by a call to Scan.
The underlying array may point to data that will be overwritten
by a subsequent call to Scan. It does no allocation.
</p>
<h3 id="Scanner.Err">func (*Scanner) <a href="http://localhost:6060/src/bufio/scan.go?s=3814:3843#L80">Err</a></h3>
<pre>func (s *<a href="index.html#Scanner">Scanner</a>) Err() <a href="../builtin/index.html#error">error</a></pre>
<p>
Err returns the first non-EOF error that was encountered by the Scanner.
</p>
<h3 id="Scanner.Scan">func (*Scanner) <a href="http://localhost:6060/src/bufio/scan.go?s=5433:5462#L118">Scan</a></h3>
<pre>func (s *<a href="index.html#Scanner">Scanner</a>) Scan() <a href="../builtin/index.html#bool">bool</a></pre>
<p>
Scan advances the Scanner to the next token, which will then be
available through the Bytes or Text method. It returns false when the
scan stops, either by reaching the end of the input or an error.
After Scan returns false, the Err method will return any error that
occurred during scanning, except that if it was io.EOF, Err
will return nil.
Scan panics if the split function returns 100 empty tokens without
advancing the input. This is a common error mode for scanners.
</p>
<h3 id="Scanner.Split">func (*Scanner) <a href="http://localhost:6060/src/bufio/scan.go?s=9072:9112#L259">Split</a></h3>
<pre>func (s *<a href="index.html#Scanner">Scanner</a>) Split(split <a href="index.html#SplitFunc">SplitFunc</a>)</pre>
<p>
Split sets the split function for the Scanner.
The default split function is ScanLines.
</p>
<p>
Split panics if it is called after scanning has started.
</p>
<h3 id="Scanner.Text">func (*Scanner) <a href="http://localhost:6060/src/bufio/scan.go?s=4262:4293#L96">Text</a></h3>
<pre>func (s *<a href="index.html#Scanner">Scanner</a>) Text() <a href="../builtin/index.html#string">string</a></pre>
<p>
Text returns the most recent token generated by a call to Scan
as a newly allocated string holding its bytes.
</p>
<h2 id="SplitFunc">type <a href="http://localhost:6060/src/bufio/scan.go?s=2770:2853#L50">SplitFunc</a></h2>
<pre>type SplitFunc func(data []<a href="../builtin/index.html#byte">byte</a>, atEOF <a href="../builtin/index.html#bool">bool</a>) (advance <a href="../builtin/index.html#int">int</a>, token []<a href="../builtin/index.html#byte">byte</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
SplitFunc is the signature of the split function used to tokenize the
input. The arguments are an initial substring of the remaining unprocessed
data and a flag, atEOF, that reports whether the Reader has no more data
to give. The return values are the number of bytes to advance the input
and the next token to return to the user, plus an error, if any. If the
data does not yet hold a complete token, for instance if it has no newline
while scanning lines, SplitFunc can return (0, nil, nil) to signal the
Scanner to read more data into the slice and try again with a longer slice
starting at the same point in the input.
</p>
<p>
If the returned error is non-nil, scanning stops and the error
is returned to the client.
</p>
<p>
The function is never called with an empty data slice unless atEOF
is true. If atEOF is true, however, data may be non-empty and,
as always, holds unprocessed text.
</p>
<h2 id="Writer">type <a href="http://localhost:6060/src/bufio/bufio.go?s=13089:13158#L501">Writer</a></h2>
<pre>type Writer struct {
<span class="comment">// contains filtered or unexported fields</span>
}</pre>
<p>
Writer implements buffering for an io.Writer object.
If an error occurs writing to a Writer, no more data will be
accepted and all subsequent writes will return the error.
After all data has been written, the client should call the
Flush method to guarantee all data has been forwarded to
the underlying io.Writer.
</p>
<div id="example_Writer" class="toggle">
<div class="collapsed">
<p class="exampleHeading toggleButton"><span class="text">Example</span></p>
</div>
<div class="expanded">
<p class="exampleHeading toggleButton"><span class="text">Example</span></p>
<p>Code:</p>
<pre class="code">w := bufio.NewWriter(os.Stdout)
fmt.Fprint(w, &#34;Hello, &#34;)
fmt.Fprint(w, &#34;world!&#34;)
w.Flush() <span class="comment">// Don&#39;t forget to flush!</span>
<span class="comment"></pre>
<p>Output:</p>
<pre class="output">Hello, world!
</pre>
</div>
</div>
<h3 id="NewWriter">func <a href="http://localhost:6060/src/bufio/bufio.go?s=13673:13708#L527">NewWriter</a></h3>
<pre>func NewWriter(w <a href="../io/index.html">io</a>.<a href="../io/index.html#Writer">Writer</a>) *<a href="index.html#Writer">Writer</a></pre>
<p>
NewWriter returns a new Writer whose buffer has the default size.
</p>
<h3 id="NewWriterSize">func <a href="http://localhost:6060/src/bufio/bufio.go?s=13354:13403#L511">NewWriterSize</a></h3>
<pre>func NewWriterSize(w <a href="../io/index.html">io</a>.<a href="../io/index.html#Writer">Writer</a>, size <a href="../builtin/index.html#int">int</a>) *<a href="index.html#Writer">Writer</a></pre>
<p>
NewWriterSize returns a new Writer whose buffer has at least the specified
size. If the argument io.Writer is already a Writer with large enough
size, it returns the underlying Writer.
</p>
<h3 id="Writer.Available">func (*Writer) <a href="http://localhost:6060/src/bufio/bufio.go?s=14467:14499#L569">Available</a></h3>
<pre>func (b *<a href="index.html#Writer">Writer</a>) Available() <a href="../builtin/index.html#int">int</a></pre>
<p>
Available returns how many bytes are unused in the buffer.
</p>
<h3 id="Writer.Buffered">func (*Writer) <a href="http://localhost:6060/src/bufio/bufio.go?s=14617:14648#L572">Buffered</a></h3>
<pre>func (b *<a href="index.html#Writer">Writer</a>) Buffered() <a href="../builtin/index.html#int">int</a></pre>
<p>
Buffered returns the number of bytes that have been written into the current buffer.
</p>
<h3 id="Writer.Flush">func (*Writer) <a href="http://localhost:6060/src/bufio/bufio.go?s=13998:14028#L540">Flush</a></h3>
<pre>func (b *<a href="index.html#Writer">Writer</a>) Flush() <a href="../builtin/index.html#error">error</a></pre>
<p>
Flush writes any buffered data to the underlying io.Writer.
</p>
<h3 id="Writer.ReadFrom">func (*Writer) <a href="http://localhost:6060/src/bufio/bufio.go?s=16620:16679#L667">ReadFrom</a></h3>
<pre>func (b *<a href="index.html#Writer">Writer</a>) ReadFrom(r <a href="../io/index.html">io</a>.<a href="../io/index.html#Reader">Reader</a>) (n <a href="../builtin/index.html#int64">int64</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
ReadFrom implements io.ReaderFrom.
</p>
<h3 id="Writer.Reset">func (*Writer) <a href="http://localhost:6060/src/bufio/bufio.go?s=13862:13897#L533">Reset</a></h3>
<pre>func (b *<a href="index.html#Writer">Writer</a>) Reset(w <a href="../io/index.html">io</a>.<a href="../io/index.html#Writer">Writer</a>)</pre>
<p>
Reset discards any unflushed buffered data, clears any error, and
resets b to write its output to w.
</p>
<h3 id="Writer.Write">func (*Writer) <a href="http://localhost:6060/src/bufio/bufio.go?s=14841:14893#L578">Write</a></h3>
<pre>func (b *<a href="index.html#Writer">Writer</a>) Write(p []<a href="../builtin/index.html#byte">byte</a>) (nn <a href="../builtin/index.html#int">int</a>, err <a href="../builtin/index.html#error">error</a>)</pre>
<p>
Write writes the contents of p into the buffer.
It returns the number of bytes written.
If nn &lt; len(p), it also returns an error explaining
why the write is short.
</p>
<h3 id="Writer.WriteByte">func (*Writer) <a href="http://localhost:6060/src/bufio/bufio.go?s=15317:15357#L603">WriteByte</a></h3>
<pre>func (b *<a href="index.html#Writer">Writer</a>) WriteByte(c <a href="../builtin/index.html#byte">byte</a>) <a href="../builtin/index.html#error">error</a></pre>
<p>
WriteByte writes a single byte.
</p>
<h3 id="Writer.WriteRune">func (*Writer) <a href="http://localhost:6060/src/bufio/bufio.go?s=15603:15659#L617">WriteRune</a></h3>
<pre>func (b *<a href="index.html#Writer">Writer</a>) WriteRune(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>)</pre>
<p>
WriteRune writes a single Unicode code point, returning
the number of bytes written and any error.
</p>
<h3 id="Writer.WriteString">func (*Writer) <a href="http://localhost:6060/src/bufio/bufio.go?s=16291:16342#L648">WriteString</a></h3>
<pre>func (b *<a href="index.html#Writer">Writer</a>) WriteString(s <a href="../builtin/index.html#string">string</a>) (<a href="../builtin/index.html#int">int</a>, <a href="../builtin/index.html#error">error</a>)</pre>
<p>
WriteString writes a string.
It returns the number of bytes written.
If the count is less than len(s), it also returns an error explaining
why the write is short.
</p>
<div id="footer">
Build version go1.6.<br>
Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
the content of this page is licensed under the
Creative Commons Attribution 3.0 License,
and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
<a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
<a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
</div>
</div><!-- .container -->
</div><!-- #page -->
<!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
<script type="text/javascript" src="../../lib/godoc/jquery.js"></script>
<script type="text/javascript" src="../../lib/godoc/jquery.treeview.js"></script>
<script type="text/javascript" src="../../lib/godoc/jquery.treeview.edit.js"></script>
<script type="text/javascript" src="../../lib/godoc/godocs.js"></script>
</body>
</html>