bytes
31 分钟阅读
Package bytes implements functions for the manipulation of byte slices. It is analogous to the facilities of the strings package.
bytes
包实现了操作字节切片的功能。它类似于strings
包的功能。
常量
|
|
MinRead is the minimum slice size passed to a Read call by Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond what is required to hold the contents of r, ReadFrom will not grow the underlying buffer.
MinRead
是Buffer.ReadFrom()
方法在调用Buffer.Read()
时传递的最小切片大小。只要Buffer
至少有MinRead
字节超出了r
内容的所需的空间,Buffer.ReadFrom()
就不会增长底层的缓冲区。
变量
|
|
ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
如果无法分配内存以存储缓冲区中的数据,ErrTooLarge
将被传递给panic。
函数
func Clone <- go1.20
|
|
Clone returns a copy of b[:len(b)]. The result may have additional unused capacity. Clone(nil) returns nil.
Clone
函数返回b[:len(b)]
的副本。其结果可能有额外的未使用的容量。Clone(nil)
返回nil。
func Compare
|
|
Compare returns an integer comparing two byte slices lexicographically. The result will be 0 if a == b, -1 if a < b, and +1 if a > b. A nil argument is equivalent to an empty slice.
Compare
函数返回一个整数,按字典顺序比较两个字节切片。如果a == b
,结果是0
,如果a < b
,结果是-1
,如果a > b
,结果是+1
。nil实参等效于空切片。
Compare Example
|
|
Compare Example (Search)
|
|
func Contains
|
|
Contains reports whether subslice is within b.
Contains
函数报告子字节切片subslice
是否在字节切片b
中。
Contains Example
|
|
func ContainsAny <- go1.7
|
|
ContainsAny reports whether any of the UTF-8-encoded code points in chars are within b.
ContainsAny
函数报告chars
中是否存在任何一个使用UTF-8编码的码点在字节切片b
中。
ContainsAny Example
|
|
func ContainsFunc <-go1.21.0
|
|
ContainsFunc reports whether any of the UTF-8-encoded code points r within b satisfy f(r).
ContainsFunc
函数报告b
中的任何UTF-8编码的码点r
是否满足f(r)
。
func ContainsRune <- go1.7
|
|
ContainsRune reports whether the rune is contained in the UTF-8-encoded byte slice b.
ContainsRune
函数报告符文r
是否包含在UTF-8编码的字节切片b
中。
ContainsRune Example
|
|
func Count
|
|
Count counts the number of non-overlapping instances of sep in s. If sep is an empty slice, Count returns 1 + the number of UTF-8-encoded code points in s.
Count
函数计算s
中sep
的非重叠实例数。如果 sep
是一个空切片,则 Count
返回 s
中 UTF-8 编码的码点数加 1
。
Count Example
|
|
func Cut <- go1.18
|
|
Cut slices s around the first instance of sep, returning the text before and after sep. The found result reports whether sep appears in s. If sep does not appear in s, cut returns s, nil, false.
Cut
函数将 s
切片在第一个 sep
的实例处分割,返回 sep
前面和后面的文本。 found
的结果报告sep
是否出现在s中。如果 sep
不出现在 s
中,则 Cut
函数返回 s, nil, false
。
Cut returns slices of the original slice s, not copies.
Cut
函数返回原始切片s
的切片,而不是副本。
Cut Example
|
|
func CutPrefix <- go1.20
|
|
CutPrefix returns s without the provided leading prefix byte slice and reports whether it found the prefix. If s doesn’t start with prefix, CutPrefix returns s, false. If prefix is the empty byte slice, CutPrefix returns s, true.
CutPrefix
函数返回没有提供前缀 prefix
的 s
,并报告它是否找到了前缀。如果 s
不以 prefix
开头,则 CutPrefix
函数返回 s, false
。如果 prefix
是空字节切片,则 CutPrefix
函数返回 s, true
。
CutPrefix returns slices of the original slice s, not copies.
CutPrefix
函数返回原始切片s
的切片,而不是副本。
CutPrefix Example
|
|
func CutSuffix <- go1.20
|
|
CutSuffix returns s without the provided ending suffix byte slice and reports whether it found the suffix. If s doesn’t end with suffix, CutSuffix returns s, false. If suffix is the empty byte slice, CutSuffix returns s, true.
CutSuffix
函数返回没有提供后缀字节切片的s
,并报告它是否找到了后缀。如果s
没有以后缀结束,CutSuffix
返回s, false
。如果后缀是空字节切片,CutSuffix
返回s,true
。
CutSuffix
函数返回没有提供后缀 suffix
的 s
,并报告它是否找到了后缀。如果 s
不以 suffix
结尾,则 CutSuffix
返回 s, false
。如果 suffix
是空字节切片,则 CutSuffix
函数返回 s, true
。
CutSuffix returns slices of the original slice s, not copies.
CutSuffix
函数返回原始切片s
的切片,而不是副本。
CutSuffix Example
|
|
func Equal
|
|
Equal reports whether a and b are the same length and contain the same bytes. A nil argument is equivalent to an empty slice.
Equal
函数报告 a
和 b
是否具有相同的长度并包含相同的字节。nil 实参等价于空切片。
Equal Example
|
|
func EqualFold
|
|
EqualFold reports whether s and t, interpreted as UTF-8 strings, are equal under simple Unicode case-folding, which is a more general form of case-insensitivity.
EqualFold
函数报告将 s
和 t
(解释为 UTF-8 字符串)在简单的 Unicode 大小写折叠下是否相等,这是一种更通用的不区分大小写的形式。
EqualFold Example
|
|
func Fields
|
|
Fields interprets s as a sequence of UTF-8-encoded code points. It splits the slice s around each instance of one or more consecutive white space characters, as defined by unicode.IsSpace, returning a slice of subslices of s or an empty slice if s contains only white space.
Fields
函数将s
解释为UTF-8编码的码点序列,并根据unicode.IsSpace
定义的一个或多个连续的空白字符实例分隔切片s
,返回s
的子切片的切片,如果s
仅包含空白,则返回一个空切片。
Fields Example
|
|
func FieldsFunc
|
|
FieldsFunc interprets s as a sequence of UTF-8-encoded code points. It splits the slice s at each run of code points c satisfying f(c) and returns a slice of subslices of s. If all code points in s satisfy f(c), or len(s) == 0, an empty slice is returned.
FieldsFunc
函数将s
解释为UTF-8编码的码点序列,并根据满足f(c)
的码点c的每个运行位置分隔切片s
,并返回s
的子切片。如果s
中所有码点都满足f(c)
,或者len(s) == 0
,则返回一个空切片。
FieldsFunc makes no guarantees about the order in which it calls f(c) and assumes that f always returns the same value for a given c.
FieldsFunc
函数对于调用f(c)
的顺序没有保证,并且假定对于给定的c
,f
始终返回相同的值。
需要注意的是,bytes.FieldsFunc函数对于调用f(c)的顺序没有保证,这是因为FieldsFunc函数是并行调用f(c)的,以便更快地处理输入。
FieldsFunc Example
|
|
func HasPrefix
|
|
HasPrefix tests whether the byte slice s begins with prefix.
HasPrefix
测试字节切片 s
是否以前缀 prefix
开头。
HasPrefix Example
|
|
func HasSuffix
|
|
HasSuffix tests whether the byte slice s ends with suffix.
HasSuffix
函数测试字节切片s
是否以后缀 suffix
结尾。
HasSuffix Example
|
|
func Index
|
|
Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
Index
函数返回sep
在s
中第一次出现的索引,如果sep
不在s中,则返回-1
。
Index Example
|
|
func IndexAny
|
|
IndexAny interprets s as a sequence of UTF-8-encoded Unicode code points. It returns the byte index of the first occurrence in s of any of the Unicode code points in chars. It returns -1 if chars is empty or if there is no code point in common.
IndexAny
函数将s
解释为UTF-8编码的Unicode码点序列。它返回chars
中任何一个Unicode码点在s
中第一次出现的字节索引。如果chars
为空或在s
中没有公共的码点,则返回-1
。
IndexAny Example
|
|
func IndexByte
|
|
IndexByte returns the index of the first instance of c in b, or -1 if c is not present in b.
IndexByte
函数返回c
在b
中第一次出现的索引,如果c
不在b
中,则返回-1
。
IndexByte Example
|
|
func IndexFunc
|
|
IndexFunc interprets s as a sequence of UTF-8-encoded code points. It returns the byte index in s of the first Unicode code point satisfying f(c), or -1 if none do.
IndexFunc
函数将s
解释为UTF-8编码的码点序列。它返回s
中第一个满足f(c)
的Unicode码点的字节索引,如果没有则返回-1
。
IndexFunc Example
|
|
func IndexRune
|
|
IndexRune interprets s as a sequence of UTF-8-encoded code points. It returns the byte index of the first occurrence in s of the given rune. It returns -1 if rune is not present in s. If r is utf8.RuneError, it returns the first instance of any invalid UTF-8 byte sequence.
IndexRune
函数将s
解释为UTF-8编码的码点序列。它返回给定符文在s
中第一次出现的字节索引。如果rune
不存在于s
中,则返回-1
。如果r
是utf8.RuneError
,则返回任何无效UTF-8字节序列的第一个实例。
IndexRune Example
|
|
func Join
|
|
Join concatenates the elements of s to create a new byte slice. The separator sep is placed between elements in the resulting slice.
Join
函数将s
中的元素连接起来以创建一个新的字节切片。分隔符sep
放置在结果切片中的元素之间。
Join Example
|
|
func LastIndex
|
|
LastIndex returns the index of the last instance of sep in s, or -1 if sep is not present in s.
LastIndex
函数返回sep
在s
中最后一次出现的索引,如果sep
不在s
中,则返回-1
。
LastIndex Example
|
|
func LastIndexAny
|
|
LastIndexAny interprets s as a sequence of UTF-8-encoded Unicode code points. It returns the byte index of the last occurrence in s of any of the Unicode code points in chars. It returns -1 if chars is empty or if there is no code point in common.
LastIndexAny
函数将s
解释为UTF-8编码的Unicode码点序列。它返回s
中任何一个Unicode代码点(chars
中的)的最后一次出现的字节索引。如果chars
为空或没有共同码点,则返回-1
。
LastIndexAny Example
|
|
func LastIndexByte <- go1.5
|
|
LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
LastIndexByte
函数返回c
在s
中最后一次出现的实例索引,如果c
不在s
中,则返回-1
。
LastIndexByte Example
|
|
func LastIndexFunc
|
|
LastIndexFunc interprets s as a sequence of UTF-8-encoded code points. It returns the byte index in s of the last Unicode code point satisfying f(c), or -1 if none do.
LastIndexFunc
将s
解释为UTF-8编码的码点序列。它返回s
中最后一个满足f(c)
的Unicode码点(rune)的字节索引,如果没有符合条件的,则返回-1
。
LastIndexFunc Example
|
|
func Map
|
|
Map returns a copy of the byte slice s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the byte slice with no replacement. The characters in s and the output are interpreted as UTF-8-encoded code points.
Map
函数返回一个字节切片s
的副本,其中所有字符根据映射函数进行修改。如果映射函数返回负值,则从字节切片中删除该字符而不作替换。s
和输出中的字符被解释为UTF-8编码的码点。
Map Example
|
|
func Repeat
|
|
Repeat returns a new byte slice consisting of count copies of b.
Repeat
函数返回一个新的字节切片,其中包含count
个b
的副本。
It panics if count is negative or if the result of (len(b) * count) overflows.
如果count
为负数或者(len(b) * count)
的结果溢出,它就会发生panic。
Repeat Example
|
|
func Replace
|
|
Replace returns a copy of the slice s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the slice and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune slice. If n < 0, there is no limit on the number of replacements.
Replace
函数返回一个切片s
的副本,用new
替换old
的前n
个不重叠的实例。如果old
是空的,它在切片的开头和每个UTF-8序列之后进行匹配,对于一个k-rune
切片,最多产生k+1
个替换。如果n<0
,则对替换的数量没有限制。
Replace Example
|
|
func ReplaceAll <- go1.12
|
|
ReplaceAll returns a copy of the slice s with all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the slice and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune slice.
ReplaceAll
函数返回将 s
中所有非重叠的 old
替换为 new
后得到的新 byte
切片。如果 old
是空的,则匹配从切片开头和每个 UTF-8 序列之后开始,最多得到 k+1
次替换,其中 k
是切片中的 Unicode 码点数。
ReplaceAll Example
|
|
func Runes
|
|
Runes interprets s as a sequence of UTF-8-encoded code points. It returns a slice of runes (Unicode code points) equivalent to s.
Runes
函数将 s
解释为一系列 UTF-8 编码的码点。它返回一个等价于 s
的码点切片(Unicode 码点)。
Runes Example
|
|
func Split
|
|
Split slices s into all subslices separated by sep and returns a slice of the subslices between those separators. If sep is empty, Split splits after each UTF-8 sequence. It is equivalent to SplitN with a count of -1.
Split
函数将s
切成由sep
分隔的所有子片,并返回这些分隔符之间的子片的一个切片。如果sep
为空,Split
会在每个UTF-8序列之后进行分割。它等同于SplitN
,计数为-1
。
To split around the first instance of a separator, see Cut.
要围绕第一个分隔符进行分割,请参见 Cut。
Split Example
|
|
func SplitAfter
|
|
SplitAfter slices s into all subslices after each instance of sep and returns a slice of those subslices. If sep is empty, SplitAfter splits after each UTF-8 sequence. It is equivalent to SplitAfterN with a count of -1.
SplitAfter
函数将 s
切分成在每个 sep
实例之后的所有子切片,并返回这些子切片的切片。如果 sep
是空的,则 SplitAfter
在每个 UTF-8 序列之后分隔。它等同于SplitAfterN
,计数为-1
。
SplitAfter Example
|
|
func SplitAfterN
|
|
SplitAfterN slices s into subslices after each instance of sep and returns a slice of those subslices. If sep is empty, SplitAfterN splits after each UTF-8 sequence. The count determines the number of subslices to return:
SplitAfterN
将 s
切分成每个 sep
实例之后的子切片,并返回这些子切片的切片。如果 sep
是空的,则 SplitAfterN
在每个 UTF-8 序列之后分隔。n
决定要返回的子切片数:
|
|
SplitAfterN Example
|
|
func SplitN
|
|
SplitN slices s into subslices separated by sep and returns a slice of the subslices between those separators. If sep is empty, SplitN splits after each UTF-8 sequence. The count determines the number of subslices to return:
SplitN
函数将 s
切分成由 sep
分隔的子切片,并返回这些分隔符之间的子切片的切片。如果 sep
是空的,则 SplitN
在每个 UTF-8 序列之后分隔。n
决定要返回的子切片数:
|
|
To split around the first instance of a separator, see Cut.
要围绕第一个分隔符进行分割,请参见 Cut。
SplitN Example
|
|
func Title <- DEPRECATED
|
|
Title treats s as UTF-8-encoded bytes and returns a copy with all Unicode letters that begin words mapped to their title case.
Title
将s
视为UTF-8编码的字节,并返回其副本,其中所有作为单词开头的Unicode字母都被转换为标题格式。
Deprecated: The rule Title uses for word boundaries does not handle Unicode punctuation properly. Use golang.org/x/text/cases instead.
已弃用:Title
用于确定单词边界的规则无法正确处理Unicode标点符号。请使用golang.org/x/text/cases
包代替。
Title Example
|
|
func ToLower
|
|
ToLower returns a copy of the byte slice s with all Unicode letters mapped to their lower case.
ToLower
函数返回一个将 s
中所有 Unicode 字母都转为其小写形式的新字节切片。
ToLower Example
|
|
func ToLowerSpecial
|
|
ToLowerSpecial treats s as UTF-8-encoded bytes and returns a copy with all the Unicode letters mapped to their lower case, giving priority to the special casing rules.
ToLowerSpecial
函数将 s
视为 UTF-8 编码的字节切片,返回一个将其中所有 Unicode 字母都转为其小写形式的新字节切片,其规则按照特定的 casing
规则进行。
ToLowerSpecial Example
|
|
func ToTitle
|
|
ToTitle treats s as UTF-8-encoded bytes and returns a copy with all the Unicode letters mapped to their title case.
ToTitle
函数将 s
视为 UTF-8 编码的字节切片,返回一个将其中所有 Unicode 字母都转为其 title case 形式的新字节切片。
ToTitle Example
|
|
func ToTitleSpecial
|
|
ToTitleSpecial treats s as UTF-8-encoded bytes and returns a copy with all the Unicode letters mapped to their title case, giving priority to the special casing rules.
ToTitleSpecia
函数将 s
视为 UTF-8 编码的字节切片,返回一个将其中所有 Unicode 字母都转为其 title case 形式的新字节切片,其规则按照特定的 casing
规则进行。
ToTitleSpecial Example
|
|
func ToUpper
|
|
ToUpper returns a copy of the byte slice s with all Unicode letters mapped to their upper case.
ToUpper
函数返回一个将 s
中所有 Unicode 字母都转为其大写形式的新字节切片。
bytes.ToUpper
和bytes.ToTitle
都是 bytes 包提供的字符串转换方法,其主要区别在于转换的方式不同。
bytes.ToUpper
方法将字符串中的所有字母字符转换成大写形式,并返回转换后的新字符串。其转换方式是基于简单的 ASCII 字符编码表进行的,不考虑各个字符在 Unicode 编码中的位置或语言特定的大小写规则。
bytes.ToTitle
方法将字符串中的所有字母字符转换成标题形式,并返回转换后的新字符串。它按照 Unicode 标准中的特定规则进行大小写转换,具体来说,它将每个字母转换成其 Unicode 标题形式。这个标题形式与普通大写形式有时会有所不同,比如德语中的 “ß” 字符,它的标题形式是 “SS”。 因此,
bytes.ToTitle
方法在进行字符大小写转换时比bytes.ToUpper
更加智能和准确。但是,由于其对 Unicode 的完全支持,可能会导致一些性能问题,特别是在处理非常大的字符串时。
ToUpper Example
|
|
func ToUpperSpecial
|
|
ToUpperSpecial treats s as UTF-8-encoded bytes and returns a copy with all the Unicode letters mapped to their upper case, giving priority to the special casing rules.
ToUpperSpecial
函数将 s
视为 UTF-8 编码的字节切片,返回一个将其中所有 Unicode 字母都转为其大写形式的新字节切片,其规则按照特定的 casing 规则进行。
ToUpperSpecial Example
|
|
func ToValidUTF8 <- go1.13
|
|
ToValidUTF8 treats s as UTF-8-encoded bytes and returns a copy with each run of bytes representing invalid UTF-8 replaced with the bytes in replacement, which may be empty.
ToValidUTF8
函数将 s
视为 UTF-8 编码的字节并返回一份副本,其中每个表示无效 UTF-8 的字节序列被替换为 replacement
中的字节,replacement
可以为空。
func Trim
|
|
Trim returns a subslice of s by slicing off all leading and trailing UTF-8-encoded code points contained in cutset.
Trim
函数返回一个将 s
去掉开头和结尾的包含在 cutset
中的所有 UTF-8 编码的码点后的新字节切片。
Trim Example
|
|
func TrimFunc
|
|
TrimFunc returns a subslice of s by slicing off all leading and trailing UTF-8-encoded code points c that satisfy f(c).
TrimFunc
函数返回一个将 s
去掉开头和结尾的所有满足 f(c)
的 UTF-8 编码的码点后的新字节切片。
TrimFunc Example
|
|
func TrimLeft
|
|
TrimLeft returns a subslice of s by slicing off all leading UTF-8-encoded code points contained in cutset.
TrimLeft
函数返回一个去除了s
中前部包含在cutset
中的Unicode编码字符的子字节切片。
TrimLeft Example
|
|
func TrimLeftFunc
|
|
TrimLeftFunc treats s as UTF-8-encoded bytes and returns a subslice of s by slicing off all leading UTF-8-encoded code points c that satisfy f(c).
TrimLeftFunc
函数返回一个去除了s
中前部符合函数f(c)
的Unicode编码字符的子字节切片。
TrimLeftFunc Example
|
|
func TrimPrefix <- go1.1
|
|
TrimPrefix returns s without the provided leading prefix string. If s doesn’t start with prefix, s is returned unchanged.
TrimPrefix
函数返回一个去除了s
中前缀prefix
的字节切片。如果s
不是以prefix
开头,则返回s
本身。
TrimPrefix Example
|
|
func TrimRight
|
|
TrimRight returns a subslice of s by slicing off all trailing UTF-8-encoded code points that are contained in cutset.
TrimRight
函数返回一个去除了s
中尾部包含在cutset
中的Unicode编码字符的子字节切片。
TrimRight Example
|
|
func TrimRightFunc
|
|
TrimRightFunc returns a subslice of s by slicing off all trailing UTF-8-encoded code points c that satisfy f(c).
TrimRightFunc
函数返回一个 s
的子切片,该子切片去除了满足 f(c)
的所有后缀 UTF-8 编码的码点。
TrimRightFunc Example
|
|
func TrimSpace
|
|
TrimSpace returns a subslice of s by slicing off all leading and trailing white space, as defined by Unicode.
TrimSpace
函数返回 s
的子切片,该子切片去除了所有的前导和尾随 Unicode 定义的空格。
TrimSpace Example
|
|
func TrimSuffix <- go1.1
|
|
TrimSuffix returns s without the provided trailing suffix string. If s doesn’t end with suffix, s is returned unchanged.
TrimSuffix
函数返回不包含指定后缀的字节切片 s
。如果 s
不以后缀结尾,则返回未更改的 s
。
TrimSuffix Example
|
|
类型
type Buffer
|
|
A Buffer is a variable-sized buffer of bytes with Read and Write methods. The zero value for Buffer is an empty buffer ready to use.
Buffer
是一个可变大小的字节缓冲区,具有Read
和Write
方法。Buffer
的零值是一个空缓冲区,可以直接使用。
Reader实现了io.Reader、io.ReaderAt、io.WriterTo、io.Seeker、io.ByteScanner和io.RuneScanner接口,通过从一个字节切片中读取数据。与Buffer不同,Reader是只读的并支持寻址。Reader的零值类似于一个空切片的Reader。
Buffer Example
|
|
Buffer Example(Reader)
|
|
func NewBuffer
|
|
NewBuffer creates and initializes a new Buffer using buf as its initial contents. The new Buffer takes ownership of buf, and the caller should not use buf after this call. NewBuffer is intended to prepare a Buffer to read existing data. It can also be used to set the initial size of the internal buffer for writing. To do that, buf should have the desired capacity but a length of zero.
NewBuffer
函数使用buf
作为其初始内容创建并初始化一个新的Buffer
。新的Buffer
接管了buf
,并且调用方不应在此调用之后使用buf
。NewBuffer
用于准备从现有数据中读取数据的缓冲区。它还可用于设置写入的内部缓冲区的初始大小。为此,buf
应该具有所需的容量,但长度为零。
In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.
在大多数情况下,new(Buffer)
(或仅声明一个Buffer
变量)足以初始化一个Buffer
。
func NewBufferString
|
|
NewBufferString creates and initializes a new Buffer using string s as its initial contents. It is intended to prepare a buffer to read an existing string.
NewBufferString
函数使用字符串s作为其初始内容创建并初始化一个新的Buffer。它用于准备从现有字符串中读取数据。
In most cases, new(Buffer) (or just declaring a Buffer variable) is sufficient to initialize a Buffer.
在大多数情况下,new(Buffer)
(或仅声明一个Buffer
变量)足以初始化一个Buffer
。
(*Buffer)Available <- go1.21.0
|
|
Available returns how many bytes are unused in the buffer.
Available
方法返回缓冲区未使用的字节数。
(*Buffer) AvailableBuffer <-go1.21.0
|
|
AvailableBuffer returns an empty buffer with b.Available() capacity. This buffer is intended to be appended to and passed to an immediately succeeding Write call. The buffer is only valid until the next write operation on b.
AvailableBuffer
方法返回一个具有b.Available()
容量的空缓冲区。此缓冲区旨在被追加并传递给紧接着的Write
调用。缓冲区仅在b
的下一个写入操作之前有效。
AvailableBuffer Example
|
|
(*Buffer) Bytes
|
|
Bytes returns a slice of length b.Len() holding the unread portion of the buffer. The slice is valid for use only until the next buffer modification (that is, only until the next call to a method like Read, Write, Reset, or Truncate). The slice aliases the buffer content at least until the next buffer modification, so immediate changes to the slice will affect the result of future reads.
Bytes
方法返回一个长度为b.Len()
的切片,其中包含缓冲区未读部分。切片仅在下一次缓冲区修改之前有效(即只在下一次像Read
、Write
、Reset
或Truncate
这样的方法调用之前有效)。切片至少与缓冲区内容同步,直到下一次缓冲区修改,因此立即更改切片将影响将来读取的结果。
Bytes Example
|
|
(*Buffer) Cap <- go1.5
|
|
Cap returns the capacity of the buffer’s underlying byte slice, that is, the total space allocated for the buffer’s data.
Cap
方法返回缓冲区底层字节切片的容量,即分配给缓冲区数据的总空间。
Cap Example
|
|
(*Buffer) Grow <- go1.1
|
|
Grow grows the buffer’s capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to the buffer without another allocation. If n is negative, Grow will panic. If the buffer can’t grow it will panic with ErrTooLarge.
Grow
方法增加缓冲区的容量(必要时),以保证另外n
个字节的空间。调用Grow(n)
之后,可以将至少n
个字节写入缓冲区,而不需要另一个分配。如果n
为负数,Grow
方法将发生panic。如果缓冲区无法增长,它将发生ErrTooLarge
的panic。
Grow Example
|
|
(*Buffer) Len
|
|
Len returns the number of bytes of the unread portion of the buffer; b.Len() == len(b.Bytes()).
Len
方法返回缓冲区未读部分的字节数;b.Len() == len(b.Bytes())
。
Len Example
|
|
(*Buffer) Next
|
|
Next returns a slice containing the next n bytes from the buffer, advancing the buffer as if the bytes had been returned by Read. If there are fewer than n bytes in the buffer, Next returns the entire buffer. The slice is only valid until the next call to a read or write method.
Next
方法返回一个包含从缓冲区中取出的下一个 n
个字节的切片,并将缓冲区推进,就像这些字节已经被 Read
返回一样。如果缓冲区中的字节数少于 n
,则 Next
方法 返回整个缓冲区。该切片只在下一次读或写方法调用之前有效。
Next Example
|
|
(*Buffer) Read
|
|
Read reads the next len(p) bytes from the buffer or until the buffer is drained. The return value n is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil.
Read
方法从缓冲区读取 len(p)
个字节或直到缓冲区被耗尽。返回值 n
是读取的字节数。如果缓冲区没有数据可返回,则 err
为 io.EOF
, len(p) == 0
否则为 nil。
Read Example
|
|
(*Buffer) ReadByte
|
|
ReadByte reads and returns the next byte from the buffer. If no byte is available, it returns error io.EOF.
ReadByte
方法从缓冲区读取并返回下一个字节。如果没有字节可用,则返回错误 io.EOF
。
ReadByte Example
|
|
(*Buffer) ReadBytes
|
|
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.
ReadBytes
方法从输入中读取,直到遇到第一个分隔符 delim
,返回包含数据和分隔符的切片。如果在找到分隔符之前遇到错误,则返回在错误之前读取的数据和错误本身(通常是 io.EOF
)。如果返回的数据不以 delim
结尾,则 ReadBytes
返回 err != nil
。
(*Buffer) ReadFrom
|
|
ReadFrom reads data from r until EOF and appends it to the buffer, growing the buffer as needed. The return value n is the number of bytes read. Any error except io.EOF encountered during the read is also returned. If the buffer becomes too large, ReadFrom will panic with ErrTooLarge.
ReadFrom
方法从 r
中读取数据,直到遇到 EOF
,然后将其附加到缓冲区中,并根据需要增加缓冲区的大小
。返回值 n
是读取的字节数。读取期间遇到的除 io.EOF
以外的任何错误也将被返回。如果缓冲区变得太大,ReadFrom
方法将使用 ErrTooLarge
引发 panic。
(*Buffer) ReadRune
|
|
ReadRune reads and returns the next UTF-8-encoded Unicode code point from the buffer. If no bytes are available, the error returned is io.EOF. If the bytes are an erroneous UTF-8 encoding, it consumes one byte and returns U+FFFD, 1.
ReadRune
方法从缓冲区读取并返回下一个 UTF-8 编码的 Unicode 码点。如果没有字节可用,则返回的错误是 io.EOF。如果字节是错误的 UTF-8 编码,则它会消耗一个字节并返回 U+FFFD,1
。
(*Buffer) ReadString
|
|
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.
ReadString
方法从输入中读取,直到遇到第一个分隔符 delim
,返回包含数据和分隔符的字符串。如果在找到分隔符之前遇到错误,则返回在错误之前读取的数据和错误本身(通常是 io.EOF
)。如果返回的数据不以 delim
结尾,则 ReadString
返回 err != nil
。
(*Buffer) Reset
|
|
Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes. Reset is the same as Truncate(0).
Reset
方法将缓冲区重置为空,但保留底层存储空间供未来写入使用。Reset
方法相当于Truncate(0)
。
(*Buffer) String
|
|
String returns the contents of the unread portion of the buffer as a string. If the Buffer is a nil pointer, it returns “
String
方法返回缓冲区未读部分的内容作为字符串。如果Buffer
是nil指针,则返回"<nil>
"。
To build strings more efficiently, see the strings.Builder type.
为了更有效地构建字符串,可以使用strings.Builder
类型。
(*Buffer) Truncate
|
|
Truncate discards all but the first n unread bytes from the buffer but continues to use the same allocated storage. It panics if n is negative or greater than the length of the buffer.
Truncate
方法将缓冲区保留未读的前n
个字节,但继续使用相同的分配存储空间。如果n
为负数或大于缓冲区的长度,则会引发panic。
(*Buffer) UnreadByte
|
|
UnreadByte unreads the last byte returned by the most recent successful read operation that read at least one byte. If a write has happened since the last read, if the last read returned an error, or if the read read zero bytes, UnreadByte returns an error.
UnreadByte
方法撤消最近一次成功读取至少一个字节的读操作返回的最后一个字节。如果自上次读取以来已经发生写入,或者上次读取返回错误,或者读取了零个字节,则UnreadByte
会返回错误。
(*Buffer) UnreadRune
|
|
UnreadRune unreads the last rune returned by ReadRune. If the most recent read or write operation on the buffer was not a successful ReadRune, UnreadRune returns an error. (In this regard it is stricter than UnreadByte, which will unread the last byte from any read operation.)
UnreadRune
方法撤消ReadRune
方法返回的最后一个符文。如果缓冲区上最近一次读取或写入操作不是成功的ReadRune
,则UnreadRune
会返回错误。(在这方面,它比UnreadByte
更严格,后者会从任何读操作中撤消最后一个字节。)
(*Buffer) Write
|
|
Write appends the contents of p to the buffer, growing the buffer as needed. The return value n is the length of p; err is always nil. If the buffer becomes too large, Write will panic with ErrTooLarge.
Write
方法将p
的内容附加到缓冲区,必要时扩展缓冲区。返回值n
是p
的长度;err
始终为nil。如果缓冲区变得太大,Write
方法会发生ErrTooLarge
的panic。
(*Buffer) WriteByte
|
|
WriteByte appends the byte c to the buffer, growing the buffer as needed. The returned error is always nil, but is included to match bufio.Writer’s WriteByte. If the buffer becomes too large, WriteByte will panic with ErrTooLarge.
WriteByte
方法将字节c
附加到缓冲区,必要时扩展缓冲区。返回的错误始终为nil,但包含在其中是为了与bufio.Writer
的WriteByte
方法匹配。如果缓冲区变得太大,WriteByte
方法会发生ErrTooLarge
的panic。
(*Buffer) WriteRune
|
|
WriteRune appends the UTF-8 encoding of Unicode code point r to the buffer, returning its length and an error, which is always nil but is included to match bufio.Writer’s WriteRune. The buffer is grown as needed; if it becomes too large, WriteRune will panic with ErrTooLarge.
WriteRune
方法将Unicode码点r
的UTF-8编码附加到缓冲区,返回其长度和错误(始终为nil,但包含在其中是为了与bufio.Writer
的WriteRune
方法匹配)。必要时扩展缓冲区;如果缓冲区变得太大,则WriteRune
方法会发生ErrTooLarge
的panic。
(*Buffer) WriteString
|
|
WriteString appends the contents of s to the buffer, growing the buffer as needed. The return value n is the length of s; err is always nil. If the buffer becomes too large, WriteString will panic with ErrTooLarge.
WriteString
方法将s
的内容附加到缓冲区,必要时扩展缓冲区。返回值n
是s
的长度;err
总是nil。如果缓冲区变得太大,WriteString
方法会发生ErrTooLarge
的panic。
(*Buffer) WriteTo
|
|
WriteTo writes data to w until the buffer is drained or an error occurs. The return value n is the number of bytes written; it always fits into an int, but it is int64 to match the io.WriterTo interface. Any error encountered during the write is also returned.
WriteTo
方法将缓冲区中的数据写入w
中,直到缓冲区被耗尽或出现错误。返回值n
为写入的字节数;它总是能够适合int
,但是为了匹配io.WriterTo
接口,它是int64
类型。任何在写入过程中遇到的错误也会被返回。
type Reader
|
|
A Reader implements the io.Reader, io.ReaderAt, io.WriterTo, io.Seeker, io.ByteScanner, and io.RuneScanner interfaces by reading from a byte slice. Unlike a Buffer, a Reader is read-only and supports seeking. The zero value for Reader operates like a Reader of an empty slice.
Reader
通过从一个字节切片读取实现io.Reader
、io.ReaderAt
、io.WriterTo
、io.Seeker
、io.ByteScanner
和io.RuneScanner
接口。与Buffer
不同,Reader
是只读的,并支持寻找。Reader
的零值操作方式类似于空切片的Reader
。
func NewReader
|
|
NewReader returns a new Reader reading from b.
NewReader
函数返回一个从b
读取的新Reader
。
(*Reader) Len
|
|
Len returns the number of bytes of the unread portion of the slice.
Len
方法返回未读部分的字节数。
Len Example
|
|
(*Reader) Read
|
|
Read implements the io.Reader interface.
Read
方法实现io.Reader
接口。
(*Reader) ReadAt
|
|
ReadAt implements the io.ReaderAt interface.
ReadAt
方法实现io.ReaderAt
接口。
(*Reader) ReadByte
|
|
ReadByte implements the io.ByteReader interface.
ReadByte
方法实现io.ByteReader
接口。
(*Reader) ReadRune
|
|
ReadRune implements the io.RuneReader interface.
ReadRune
方法实现io.RuneReader
接口。
(*Reader) Reset <- go1.7
|
|
Reset resets the Reader to be reading from b.
Reset
方法将Reader
重置为从b
读取。
(*Reader) Seek
|
|
Seek implements the io.Seeker interface.
Seek
方法实现io.Seeker
接口。
(*Reader) Size <- go1.5
|
|
Size returns the original length of the underlying byte slice. Size is the number of bytes available for reading via ReadAt. The result is unaffected by any method calls except Reset.
Size
方法返回底层字节切片的原始长度。Size
方法是可通过ReadAt
方法读取的字节数。该结果不受任何方法调用的影响,除了Reset
方法。
(*Reader) UnreadByte
|
|
UnreadByte complements ReadByte in implementing the io.ByteScanner interface.
UnreadByte
方法实现了 io.ByteScanner
接口,用于将上一次读取的一个字节退回到 Reader
中。
(*Reader) UnreadRune
|
|
UnreadRune complements ReadRune in implementing the io.RuneScanner interface.
UnreadRune
方法实现了 io.RuneScanner
接口,用于将上一次读取的一个符文退回到 Reader
中。
(*Reader) WriteTo <- go1.1
|
|
WriteTo implements the io.WriterTo interface.
WriteTo
方法实现了 io.WriterTo
接口,用于将 Reader
中未读取的数据写入 w
,直到读取完毕或出现错误。返回值 n
是写入的字节数,它总是可以用 int
表示,但为了匹配 io.WriterTo
接口,返回值类型为 int64
。在写入过程中遇到的任何错误也会一并返回。