Bytes Package usage in Golang

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

This article was transferred from Golove's blog http://www.cnblogs.com/golove/p/3287729.html

Functions and methods in the bytes package



The bytes package implements a function for manipulating []byte, similar to a function in a strings package

Bytes.go

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

Compare is used to compare two []byte and return the result of int type
A = = B returns 0
A < b return-1
A > B return 1
If the parameter is nil it is equivalent to passing in an empty []byte
Func Compare (A, b []byte) int

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

function similar to the same name function in the strings package
Count calculates the non-overlapping number of Sep in S
If Sep is empty, the number of characters in S is returned + 1
Func Count (S, Sep []byte) int

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

function similar to the same name function in the strings package
Contains determine if B contains Subslice
Returns True if Subslice is empty
Func Contains (b, Subslice []byte) bool

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

function similar to the same name function in the strings package
Index returns the position of the first occurrence of Sep in S
If not found, returns 1 if Sep is empty, 0 is returned
Func Index (S, Sep []byte) int

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

function similar to the same name function in the strings package
LastIndex returns the last byte position of Sep in S
If not found, returns 1 if Sep is empty, returns the length of S
Func LastIndex (S, Sep []byte) int

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

function similar to the same name function in the strings package
Indexrune returns the position of the first occurrence of the encoding of R in S
Returns 1 if it is not found
Func Indexrune (S []byte, R rune) int

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

function similar to the same name function in the strings package
Indexany returns the position of the first occurrence of any character in the chars in S
If not found, returns-1 if chars is empty, then returns-1
Func Indexany (S []byte, chars string) int

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

function similar to the same name function in the strings package
Lastindexany returns the position of the last occurrence of any one of the characters in the chars in S
If not found, returns-1, if chars is empty, also returns-1
Func Lastindexany (S []byte, chars string) int

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

function similar to the same name function in the strings package
Splitn the S tangent to multiple substrings with Sep as a delimiter, and the result does not include the SEP itself
If Sep is empty, then S is cut into a list of Unicode characters
If there is no sep in S, the entire s is returned as the first element of [][]byte]
The parameter n means the maximum number of substrings to be cut out, and the part to be exceeded will no longer be sliced
If n is 0, nil is returned, if n is less than 0, the number of slices is not limited, all slices
Func splitn (S, Sep []byte, n int) [][]byte

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

function similar to the same name function in the strings package
Splitaftern and SPLITN have the same function, except that the results include Sep
Func Splitaftern (S, Sep []byte, n int) [][]byte

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

function similar to the same name function in the strings package
Split takes Sep as a delimiter, splits s into substrings, and the result does not include the SEP itself
If Sep is empty, the S is cut into a list of Unicode characters.
If there is no sep substring in S, the entire s is returned as the first element of []string]
Func Split (S, Sep []byte) [][]byte

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

function similar to the same name function in the strings package
Splitafter and Split have the same function, except that the results include Sep
Func Splitafter (S, Sep []byte) [][]byte

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

function similar to the same name function in the strings package
fields are delimited by contiguous whitespace characters, and s are sliced into substrings, and the result does not contain the white space character itself
Whitespace characters are: \ t, \ n, \v, \f, \ R, ', u+0085 (NEL), u+00a0 (NBSP)
If s contains only white space characters, an empty list is returned
Func fields (s []byte) [][]byte

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

function similar to the same name function in the strings package
Fieldsfunc is delimited by one or more consecutive characters that satisfy F (rune),
Splits s into multiple substrings, and the result does not include the delimiter itself
Returns an empty list if the F (rune) character is not met in S
Func Fieldsfunc (S []byte, F func (rune) bool) [][]byte

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

function similar to the same name function in the strings package
Join joins the substrings in S into a single []byte, with the substrings separated by Sep
Func Join (S [][]byte, Sep []byte] []byte

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

function similar to the same name function in the strings package
Hasprefix to determine if s starts with prefix
If prefix is empty, also returns true
Func Hasprefix (s, prefix []byte) bool

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

function similar to the same name function in the strings package
Hasprefix determine if s ends with prefix
If suffix is empty, also returns true
Func hassuffix (s, suffix []byte) bool

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

function similar to the same name function in the strings package
Map replaces characters in s that meet mapping (rune) with the return value of mapping (rune)
If mapping (rune) returns a negative number, the corresponding character is deleted
Func Map (mapping func (R Rune) rune, S []byte) []byte

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

function similar to the same name function in the strings package
The Repeat joins Count B into a new []byte
Func Repeat (b []byte, Count int) []byte

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

function similar to the same name function in the strings package
ToUpper change all characters in s to their uppercase format
For non-ASCII characters, its uppercase format requires a table-check conversion
Func ToUpper (s []byte) []byte

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

function similar to the same name function in the strings package
ToLower change all characters in s to their lowercase format
For non-ASCII characters, its lowercase format requires a table-check conversion
Func ToLower (s []byte) []byte

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

function similar to the same name function in the strings package
Totitle modify all characters in S to their Title format
The Title format of most characters is its Upper format
Only a few characters of the Title format are special characters
The totitle here is primarily for the Title function call
Func Totitle (s []byte) []byte

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

function similar to the same name function in the strings package
Toupperspecial change all characters in s to their uppercase format
Preference for conversion using rules in _case
Func toupperspecial (_case Unicode. Specialcase, S []byte] []byte

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

function similar to the same name function in the strings package
Tolowerspecial change all characters in s to their lowercase format
Preference for conversion using rules in _case
Func tolowerspecial (_case Unicode. Specialcase, S []byte] []byte

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

function similar to the same name function in the strings package
Totitlespecial modify all characters in S to their Title format
Preference for conversion using rules in _case
Func totitlespecial (_case Unicode. Specialcase, S []byte] []byte

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

function similar to the same name function in the strings package
Title changes the first letter of all words in S to its title format
Bug:title rules do not handle Unicode punctuation correctly
Func Title (s []byte) []byte

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

function similar to the same name function in the strings package
Trimleftfunc will remove the S-head continuous character that satisfies F (rune)
Func Trimleftfunc (S []byte, F func (R rune) bool) []byte

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

function similar to the same name function in the strings package
Trimrightfunc will remove the S-tail continuous character that satisfies F (rune)
Func Trimrightfunc (S []byte, F func (R rune) bool) []byte

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

function similar to the same name function in the strings package
Trimfunc will remove the S-end-to-end character that satisfies F (rune)
Func Trimfunc (S []byte, F func (R rune) bool) []byte

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

function similar to the same name function in the strings package
Trimprefix Delete prefix string for s header
If s does not start with prefix, it returns the original S
Func Trimprefix (s, prefix []byte) []byte

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

function similar to the same name function in the strings package
Trimsuffix delete the suffix string at the tail of s
If s does not end with suffix, it returns the original S
Func trimsuffix (s, suffix []byte) []byte

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

function similar to the same name function in the strings package
Indexfunc returns the byte position of the first character in s that satisfies F (rune)
Returns 1 if the F (rune) character is not met
Func Indexfunc (S []byte, F func (R rune) bool) int

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

function similar to the same name function in the strings package
Lastindexfunc returns the byte position of the last character in S that satisfies F (rune)
Returns 1 if the F (rune) character is not met
Func Lastindexfunc (S []byte, F func (R rune) bool) int

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

function similar to the same name function in the strings package
Trim will remove the characters that are included in the Cutset with the S-tail
Func Trim (S []byte, cutset string) []byte

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

function similar to the same name function in the strings package
Trimleft will delete the characters in the cutset that are contained in the S header consecutively
Func Trimleft (S []byte, cutset string) []byte

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

function similar to the same name function in the strings package
TrimRight will delete the characters in the cutset that are contained in the S tail
Func TrimRight (S []byte, cutset string) []byte

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

function similar to the same name function in the strings package
Trimspace will delete the S-end continuous white space characters
Func Trimspace (s []byte) []byte

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

Runes S is cut into Unicode code point list
Func runes (s []byte) []rune

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

function similar to the same name function in the strings package
Replace replaces the old substring in s with the new substring and returns
The number of substitutions is n, and if n is-1, replace All
If old is empty, a new is inserted between each character
Func Replace (S, old, new []byte, n int) []byte

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

function similar to the same name function in the strings package
Equalfold determines if s and T are equal. Ignores case, and it also transforms special characters
For example, convert "Φ" to "Φ", "DŽ" to "DŽ", and then compare
Func Equalfold (S, t []byte) bool



============================================================



Bytes_decl.go

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

Indexbyte returns the position of the first occurrence of C in S
Returns 1 if it is not found
Func Indexbyte (s []byte, C byte) int

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

Equal determine if A and B are equal
If the parameter is nil, it is considered null []byte
Func Equal (A, b []byte) bool



============================================================



Reader.go

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

Type Reader struct {
Private fields
}

Create a bytes by []byte]. Reader Object
Func Newreader (b []byte) *reader

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

Returns the length of data not read in R
Func (R *reader) Len () int

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

Implement IO. Reader interface
Func (R *reader) Read (b []byte) (n int, err error)

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

Implement IO. Readerat interface
Func (R *reader) ReadAt (b []byte, off Int64) (n int, err error)

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

Implement IO. Bytescanner interface
Func (R *reader) ReadByte () (b byte, err error)
Func (R *reader) unreadbyte () error

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

Implement IO. Runescanner interface
Func (R *reader) readrune (Ch rune, size int, err error)
Func (R *reader) Unreadrune () error

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

Implement IO. Seeker interface
Func (R *reader) Seek (offset int64, whence int) (Int64, error)

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

Implement IO. Writerto interface
Func (R *reader) writeto (w io). Writer) (n Int64, err error)



============================================================



Buffer.go

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

Buffer implements input-output operation with cache
The cached capacity is automatically scaled as needed
If the cache is too large to continue expanding, panic (Errtoolarge) is thrown
Type Buffer struct {
Private fields
}

Create a bytes from []byte or string]. Buffer Object
Func Newbuffer (buf []byte) *buffer
Func newbufferstring (s string) *buffer

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

Returns a slice of data in B
Func (b *buffer) Bytes () []byte

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

Returns a copy of the data taken in B
Func (b *buffer) string () string

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

Returns the length of data in B
Func (b *buffer) Len () int

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

Truncate truncate the data in the buffer to the first n bytes, and the truncated portion is discarded
If n is negative or n exceeds the total buffer length, panic is raised
Func (b *buffer) Truncate (n int)

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

The Reset method is used to reset the cache by emptying the data in the cache
B.reset () is equivalent to B.truncate (0).
Func (b *buffer) Reset ()

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

Grow extends the cache length back by n bytes, without returning any data.
If n is a complex number, panic will be raised and panic (Errtoolarge) will be thrown if the cache length cannot be extended.
Func (b *buffer) Grow (n int)

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

Implementation of IO. Writer interface
Func (b *buffer) Write (P []byte) (n int, err error)

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

function with Write, except that the argument is a string type
Func (b *buffer) writestring (s string) (n int, err error)

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

Implement IO. Readerfrom interface
Func (b *buffer) readfrom (R io. Reader) (n Int64, err error)

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

Implementation of IO. Writerto interface
Func (b *buffer) writeto (w io). Writer) (n Int64, err error)

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

Implementation of IO. Bytewriter interface
Func (b *buffer) writebyte (c byte) error

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

Writerune writes a character R to the object's data stream
Returns any errors encountered during the write process
Func (b *buffer) Writerune (R rune) (n int, err error)

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

Implementation of IO. Reader interface
Func (b *buffer) Read (P []byte) (n int, err error)

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

Next reads the first n bytes of data in the cache and returns its reference
If n is greater than the length of the data in the cache, all data is read out
The data being read is valid before the next read and write operation
When the next read and write operation, the referenced data may be overwritten
Func (b *buffer) Next (n int) []byte

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

Implement IO. Bytescanner interface
Func (b *buffer) ReadByte () (c byte, err error)
Func (b *buffer) unreadbyte () error

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

Implement IO. Runescanner interface
Func (b *buffer) Readrune () (R rune, size int, err error)
Func (b *buffer) Unreadrune () error

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

Similar to the Reader.readbytes and Reader.readstring methods in the Bufio package
Readbytes finds Delim in B and reads all of the Delim and its previous data
The ReadString function is the same as readbytes, except that a string is returned
Func (b *buffer) readbytes (Delim byte) (line []byte, err Error)
Func (b *buffer) ReadString (Delim byte) (line string, err Error)





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.