Usage of BUFIO package in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

This article goes from Golove Blog: http://www.cnblogs.com/golove/p/3282667.html and on this basis, some additions and modifications are made.

Functions and methods in the Bufio package



The Bufio package implements the I/O operation with caching
It encapsulates an IO. Reader or IO. Writer Object
Make it a cache and some text read and write functions

------------------------------------------------------------

Bufio.go

------------------------------------------------------------

Type Reader struct {    buf          []byte    Rd           io. Reader//reader provided    by the client R, w         int       //buf Read and write positions    err          error    Lastby Te     int    lastrunesize int    }

Newreadersize Encapsulates Rd as a bufio that has a size cache. Reader Object
If the base type of RD is Bufio. Reader type, and has enough cache
The RD is converted directly to a base type and returned
Func newreadersize (rd IO. Reader, size int) *reader

Newreader equivalent to newreadersize (RD, 4096)
Func Newreader (rd IO. Reader) *reader

------------------------------------------------------------

Peek returns a slice of the cache that references the first n bytes of data in the cache
The operation does not read the data, just the reference
The referenced data is valid before the next read operation
If the referenced data length is less than n, an error message is returned
Returns ERRBUFFERFULL if n is greater than the total size of the cache
The data in the cache can be modified by the return value of Peek
However, you cannot modify the underlying IO. The data in Reader

Func Main () {s: = strings. Newreader ("ABCDEFG") br: = Bufio. Newreader (s) b, _: = Br. Peek (5) fmt. Printf ("%s\n", b)//abcdeb[0] = ' A ' b, _ = br. Peek (5) fmt. Printf ("%s\n", b)//ABCDE}

------------------------------------------------------------

Read reads data from B to P, returns the number of bytes read out
If the size of P >= The total size of the cache, and the cache is not empty
You can only read the data in the cache, not from the underlying IO. Extracting data from Reader
If the size of P >= The total size of the cache, and the cache is empty
Directly from the underlying IO. Reader reads data to p without caching
Returns only if there is no readable data in B (0, Io. EOF)
Func (b *reader) Read (P []byte) (n int, err error)

Func Main () {s: = strings. Newreader ("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") br: = Bufio. Newreader (s) b: = make ([]byte, C) n, err: = Br. Read (b) fmt. Printf ("%-20s%-2v%v\n", B[:n], N, err)//Abcdefghijklmnopqrst <nil>n, err = br. Read (b) fmt. Printf ("%-20s%-2v%v\n", B[:n], N, err)//UVWXYZ1234567890 <nil>n, err = br. Read (b) fmt. Printf ("%-20s%-2v%v\n", B[:n], N, err)//0 EOF}

------------------------------------------------------------

ReadByte reads a byte from B and returns
If there is no readable data in B, an error is returned
Func (b *reader) ReadByte () (c byte, err error)

Unreadbyte undo last Read-out byte
Only the last bytes read out can be undone
No matter what the operation, as long as the content is read out, you can undo a byte with Unreadbyte
Func (b *reader) unreadbyte () error

Func Main () {s: = strings. Newreader ("ABCDEFG") br: = Bufio. Newreader (s) C, _: = Br. ReadByte () fmt. Printf ("%c\n", c)//Ac, _ = br. ReadByte () fmt. Printf ("%c\n", c)//Bbr.unreadbyte () C, _ = Br. ReadByte () fmt. Printf ("%c\n", c)//B}

------------------------------------------------------------

Readrune reads a UTF8 encoded character from B and returns
Also returns the UTF8 encoding length of the character
If the UTF8 sequence cannot decode a correct Unicode character
Only one byte in B is read, and the U+FFFD character is returned, and size returns 1
Func (b *reader) Readrune () (R rune, size int, err error)

Unreadrune undo the last read-out Unicode character
Returns an error if the last time it was performed is not a readrune operation
As a result, Unreadrune is stricter than unreadbyte.
Func (b *reader) Unreadrune () error
Func Main () {s: = strings. Newreader ("Hello, world!") ") br: = Bufio. Newreader (s) c, size, _: = Br. Readrune () fmt. Printf ("%c%v\n", c, size)//You 3c, size, _ = br. Readrune () fmt. Printf ("%c%v\n", c, size)//Good 3br. Unreadrune () c, size, _ = br. Readrune () fmt. Printf ("%c%v\n", c, size)//Good 3}

------------------------------------------------------------

Buffered returns the length of the data in the cache
Func (b *reader) Buffered () IntFunc main () {s: = strings. Newreader ("Hello, world!") ") br: = Bufio. Newreader (s) fmt. Println (Br. Buffered ())//0BR. Peek (1) fmt. Println (Br. Buffered ())//18}

------------------------------------------------------------

Readslice finds Delim in B and returns a slice of all the data delim and before it
The operation reads the data, and the returned slice is a reference to the read-out data
The data in the slice is valid before the next read operation
//
If Readslice encounters an error before finding Delim
All data in the cache is read and returned, and the error encountered (usually IO) is returned. EOF)
If Delim is not found in the entire cache, ERR returns errbufferfull
If Readslice can find Delim, then err always returns nil
//
Because the data in the returned slice may be modified by the next read and write operation
So most operations should use Readbytes or ReadString, and they return not data references
Func (b *reader) readslice (Delim byte) (line []byte, err Error)
Func Main () {s: = strings. Newreader ("ABC DEF GHI JKL") br: = Bufio. Newreader (s) W, _: = Br. Readslice (') fmt. Printf ("%q\n", W)//"ABC" W, _ = br. Readslice (') fmt. Printf ("%q\n", W)//"DEF" W, _ = br. Readslice (') fmt. Printf ("%q\n", W)//"GHI"}

------------------------------------------------------------

ReadLine is a low-level primitive line read operation
In most cases, you should use Readbytes (' \ n ') or ReadString (' \ n ')
or use a Scanner
//
ReadLine is implemented by calling the Readslice method, and the cached slices are returned
ReadLine attempt to return a single row of data, excluding end-of-line markers (\ n or \ r \ n)
If the end-of-line tag is not found in the cache, set Isprefix to True to indicate that the lookup is not completed
Also reads the data from the cache and returns it as a slice
The Isprefix is set to false only if the end-of-line marker is found in the current cache, indicating that the lookup is complete
You can call ReadLine multiple times to read a line
The returned data is valid before the next read operation
If ReadLine cannot get any data, an error message (usually IO) is returned. EOF)
Func (b *reader) ReadLine (line []byte, isprefix bool, err Error)

Func Main () {s: = strings. Newreader ("Abc\ndef\r\nghi\r\njkl") br: = Bufio. Newreader (s) W, isprefix, _: = Br. ReadLine () fmt. Printf ("%q%v\n", W, Isprefix)//"ABC" Falsew, Isprefix, _ = br. ReadLine () fmt. Printf ("%q%v\n", W, Isprefix)//"DEF" Falsew, Isprefix, _ = br. ReadLine () fmt. Printf ("%q%v\n", W, Isprefix)//"GHI" false}

------------------------------------------------------------

Readbytes finds Delim in B and reads all of the Delim and its previous data
If Readbytes encounters an error before finding Delim
Returns all data before the error is encountered, and returns the error encountered (usually IO). EOF)
Err is not nil only if readbytes cannot find Delim
For simple purposes, it may be more convenient to use Scanner
Func (b *reader) readbytes (Delim byte) (line []byte, err Error)

Func Main () {s: = strings. Newreader ("ABC DEF GHI JKL") br: = Bufio. Newreader (s) W, _: = Br. Readbytes (') fmt. Printf ("%q\n", W)//"ABC" W, _ = br. Readbytes (') fmt. Printf ("%q\n", W)//"DEF" W, _ = br. Readbytes (') fmt. Printf ("%q\n", W)//"GHI"}

------------------------------------------------------------

The ReadString function is the same as readbytes, except that a string is returned
Func (b *reader) ReadString (Delim byte) (line string, err Error)

Func Main () {s: = strings. Newreader ("ABC DEF GHI JKL") br: = Bufio. Newreader (s) W, _: = Br. ReadString (') fmt. Printf ("%q\n", W)//"ABC" W, _ = br. ReadString (') fmt. Printf ("%q\n", W)//"DEF" W, _ = br. ReadString (') fmt. Printf ("%q\n", W)//"GHI"}

------------------------------------------------------------

The WriteTo implements IO. Writerto interface
Func (b *reader) writeto (w io). Writer) (n Int64, err error)
Func Main () {s: = strings. Newreader ("ABCEFG") br: = Bufio. Newreader (s) b: = bytes. Newbuffer (Make ([]byte, 0)) Br. WriteTo (b) fmt. Printf ("%s\n", b)//ABCEFG}

------------------------------------------------------------

Func (b *reader) Reset (R io. Reader)
Reset discards any cached data, planting all states and switches the cache read to R

Package Mainimport ("Bufio" "FMT" "strings") func main () {s: = strings. Newreader ("ABCEFG") str: = Strings. Newreader ("123455") br: = Bufio. Newreader (s) b, _: = Br. ReadString (' \ n ') fmt. Println (b)     //abcefgbr.reset (str) b, _ = br. ReadString (' \ n ') fmt. Println (b)     //123455}

------------------------------------------------------------



------------------------------------------------------------

Writer implements the IO with the cache. Writer Object
If you encounter an error while writing data to writer
The Writer will no longer accept any data
and subsequent write operations will return an error message
Type Writer struct {

   Err   error   buf  []byte   n     int   WR   io. Writer
}

The newwritersize encapsulates WR as a bufio with a size cache. Writer Object
If the base type of WR is Bufio. Writer type, and has enough cache
The WR is converted directly to a base type and returns
Func newwritersize (WR io. Writer, size int) *writer

Newwriter equivalent to Newwritersize (WR, 4096)
Func newwriter (WR io. Writer) *writer

------------------------------------------------------------

Flush submits the data in the cache to the underlying IO. In Writer
Func (b *writer) Flush () error

Available returns the space available in the cache
Func (b *writer) Available () int

Buffered returns the uncommitted data length in the cache
Func (b *writer) Buffered () int

Write writes the data in P to B and returns the number of bytes written
If the number of bytes written is less than the length of p, an error message is returned
Func (b *writer) Write (P []byte) (nn int, err error)

WriteString with write, except that the string is written
Func (b *writer) writestring (s string) (int, error)

Func main () {b: = bytes. Newbuffer (Make ([]byte, 0)) BW: = Bufio. Newwriter (b) fmt. PRINTLN (BW. Available ())//4096 FMT. PRINTLN (BW. Buffered ())//0 bw. WriteString ("Abcdefgh") fmt. PRINTLN (BW. Available ())//4088 FMT. PRINTLN (BW. Buffered ())//8 FMT. Printf ("%q\n", B)//"" BW. Flush () fmt. PRINTLN (BW. Available ())//4096 FMT. PRINTLN (BW. Buffered ())//0 FMT. Printf ("%q\n", B)//"ABCEFG"}

------------------------------------------------------------

WriteByte writes a byte to B
Func (b *writer) writebyte (c byte) error

Writerune writes R's UTF8 encoding to B
Returns the encoded length of R
Func (b *writer) Writerune (R rune) (size int, err error)
Func main () {b: = bytes. Newbuffer (Make ([]byte, 0)) BW: = Bufio. Newwriter (b) bw. WriteByte (' H ') bw. WriteByte (' e ') bw. WriteByte (' l ') bw. WriteByte (' l ') bw. WriteByte (' o ') bw. WriteByte (') bw. Writerune (' world ') bw. Writerune (' bounded ') bw. Writerune ('! ') bw. Flush () fmt. PRINTLN (b)//Hello world! }

------------------------------------------------------------

The Readfrom implements IO. Readerfrom interface
Func (b *writer) readfrom (R io. Reader) (n Int64, err error)

Func main () {b: = bytes. Newbuffer (Make ([]byte, 0)) s: = Strings. Newreader ("Hello world!") ") BW: = Bufio. Newwriter (b) bw. Readfrom (s)//bw. Flush ()            //readfrom does not have to use flush, its own write. Fmt. PRINTLN (b)//Hello world! }

------------------------------------------------------------

Func (b *writer) Reset (w io. Writer)
Reset discards any cached data that is not written, clears any errors, and re-assigns B to its output to W

Package Mainimport ("Bufio" "bytes" "FMT") func main () {b: = bytes. Newbuffer (Make ([]byte, 0)) BW: = Bufio. Newwriter (b) bw. WriteString ("123") c: = bytes. Newbuffer (Make ([]byte, 0)) bw. Reset (c) bw. WriteString ("456") bw. Flush () fmt. Println (b)       //output is empty FMT. PRINTLN (c)//Output 456}




------------------------------------------------------------

The Readwriter integrates the Bufio. Reader and Bufio. Writer
It implements the IO. Readwriter interface

Type readwriter struct {*reader*writer}

The Newreadwriter package R and W are a bufio. Readwriter Object
Func Newreadwriter (R *reader, W *writer) *readwriter

Package Mainimport ("Bufio" "bytes" "FMT" "strings") func main () {b: = bytes. Newbuffer (Make ([]byte, 0)) BW: = Bufio. Newwriter (b) s: = Strings. Newreader ("123") br: = Bufio. Newreader (s) RW: = Bufio. Newreadwriter (BR, BW) p, _: = RW. ReadString (' \ n ') fmt. Println (String (p))              //123RW. WriteString ("ASDF") rw. Flush () fmt. Println (b)                          //ASDF}



------------------------------------------------------------

Scan.go

------------------------------------------------------------

Scanner provides a convenient interface to read data, such as reading a multiline text
A sequential call to the scan method scans the "specified section" in the data, skipping the data between the individual "specified parts"
Scanner uses a cache, so the length of the "specified part" cannot exceed the length of the cache
Scanner requires a splitfunc type of "Shard function" to determine the format of the "specified part"
The "Shard function" provided in this package has a "row-splitting function", "byte-splitting function", "UTF8 character-encoding-splitting function"
and the word segmentation function, users can also customize the slice function
The default "Shard function" is "row splitting function", which is used to get a row of data in the data (excluding line endings)
//
The scan stops when it encounters the following conditions:
1, the data scan completed, encountered IO. Eof
2. Read/write errors encountered
3. The length of "specified part" exceeds the length of the cache
If you want more control over your data, such as error handling or scanning for larger "specified parts" or sequential scans
You should use Bufio. Reader

Type Scanner struct {r            io. Reader//The reader provided by the Client.split        splitfunc//The function to split the tokens.maxtokensize int       / /Maximum size of a token; Modified by Tests.token        []byte    //Last token returned by Split.buf          []byte    //Buffer used as argument to SP Lit.start        int       //First non-processed byte in buf.end          int       //end of data in Buf.err          error     //S Ticky error.}


Splitfunc used to define the "shard function" type
Data is what you want to scan.
Ateof marks the underlying IO. Whether the data in reader has been read out
Advance returns the length of data processed
Token returns the "specified part" found
ERR returns error message
If a complete "specified part" cannot be found in data
Then Splitfunc returns (0, nil) to tell Scanner
Populate the cache with more data and then scan again
//
If the returned err is a non-nil value, the scan is terminated and an error message is returned
//
If data is empty, the Shard function will not be called
This means that you don't have to consider the case where data is empty in Splitfunc
//
Splitfunc is a simple function of finding the data you are interested in and then returning
And tell the caller how much data there is that you've already processed.
Type splitfunc func (data []byte, ateof bool) (advance int, token []byte, err Error)

Newscanner Create a Scanner to scan R
The default Shard function is Scanlines
Func Newscanner (R io. Reader) *scanner

ERR returns non-EOF errors encountered during the scan
For the user to call in order to get error information
Func (S *scanner) ERR () error

------------------------------------------------------------

Bytes returns the last scanned "specified part" as a slice (reference pass)
The next Scan operation overwrites the results returned by this time
Func (S *scanner) Bytes () []byte

Text returns the last scanned "specified part" as a string (value pass)
Func (S *scanner) Text () string

------------------------------------------------------------

Scan scans "specified section" in Scanner data
When found, the user can use the Bytes or Text method to remove the "specified part"
Terminates the scan and returns False if an error is encountered during the scan
Func (S *scanner) Scan () bool
Func Main () {s: = strings. Newreader ("ABC\NDEF\R\NGHI\NJKL") bs: = Bufio. Newscanner (s) for BS. Scan () {fmt. Printf ("%s%v\n", BS. Bytes (), BS. Text ())}//ABC abc//DEF def//GHI ghi//JKL JKL}

------------------------------------------------------------

Split function to set the Scanner
This function must be executed before calling Scan
Func (S *scanner) split (split Splitfunc)

Func Main () {s: = strings. Newreader ("ABC DEF GHI JKL") bs: = Bufio. Newscanner (s) BS. Split (Bufio. scanwords) for BS. Scan () {fmt. PRINTLN (BS. Text ())}//abc//def//ghi//JKL}

------------------------------------------------------------

Scanbytes is a "slicing function"
Used to find a single byte in data and return
Func scanbytes (data []byte, ateof bool) (advance int, token []byte, err Error)
Func Main () {s: = strings. Newreader ("Hello world!") ") bs: = Bufio. Newscanner (s) BS. Split (Bufio. scanbytes) for BS. Scan () {fmt. Printf ("%s", BS. Text ())}}

------------------------------------------------------------

Scanrunes is a "slicing function"
Used to find the encoding of a single UTF8 character in data and return
If the UTF8 decoding error occurs, the returned U+FFFD is returned as "\XEF\XBF\XBD"
This makes it impossible for users to distinguish between "real u+fffd characters" and "Decoded error return Values"
Func scanrunes (data []byte, ateof bool) (advance int, token []byte, err Error)
Func Main () {s: = strings. Newreader ("Hello world!") ") bs: = Bufio. Newscanner (s) BS. Split (Bufio. Scanrunes) for BS. Scan () {fmt. Printf ("%s", BS. Text ())}//H e l l o world! }
------------------------------------------------------------

Scanlines is a "slicing function"
Used to find a single row of data and return (including Blank lines)
The line end tag may be \ n or \ r \ n (The return value does not include the end of line mark)
Func scanlines (data []byte, ateof bool) (advance int, token []byte, err Error)

------------------------------------------------------------

Scanwords is a "slicing function"
To find the words in data
The words are separated by white space characters, and the whitespace characters are Unicode. IsSpace definition
Func scanwords (data []byte, ateof bool) (advance int, token []byte, err Error)





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.