通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
# package bytes `import "bytes"` bytes包实现了操作[]byte的常用函数。本包的函数和strings包的函数相当类似。 ## Index * [Constants](#pkg-constants) * [Variables](#pkg-variables) * [func Compare(a, b []byte) int](#Compare) * [func Equal(a, b []byte) bool](#Equal) * [func EqualFold(s, t []byte) bool](#EqualFold) * [func Runes(s []byte) []rune](#Runes) * [func HasPrefix(s, prefix []byte) bool](#HasPrefix) * [func HasSuffix(s, suffix []byte) bool](#HasSuffix) * [func Contains(b, subslice []byte) bool](#Contains) * [func Count(s, sep []byte) int](#Count) * [func Index(s, sep []byte) int](#Index) * [func IndexByte(s []byte, c byte) int](#IndexByte) * [func IndexRune(s []byte, r rune) int](#IndexRune) * [func IndexAny(s []byte, chars string) int](#IndexAny) * [func IndexFunc(s []byte, f func(r rune) bool) int](#IndexFunc) * [func LastIndex(s, sep []byte) int](#LastIndex) * [func LastIndexAny(s []byte, chars string) int](#LastIndexAny) * [func LastIndexFunc(s []byte, f func(r rune) bool) int](#LastIndexFunc) * [func Title(s []byte) []byte](#Title) * [func ToLower(s []byte) []byte](#ToLower) * [func ToLowerSpecial(\_case unicode.SpecialCase, s []byte) []byte](#ToLowerSpecial) * [func ToUpper(s []byte) []byte](#ToUpper) * [func ToUpperSpecial(\_case unicode.SpecialCase, s []byte) []byte](#ToUpperSpecial) * [func ToTitle(s []byte) []byte](#ToTitle) * [func ToTitleSpecial(\_case unicode.SpecialCase, s []byte) []byte](#ToTitleSpecial) * [func Repeat(b []byte, count int) []byte](#Repeat) * [func Replace(s, old, new []byte, n int) []byte](#Replace) * [func Map(mapping func(r rune) rune, s []byte) []byte](#Map) * [func Trim(s []byte, cutset string) []byte](#Trim) * [func TrimSpace(s []byte) []byte](#TrimSpace) * [func TrimFunc(s []byte, f func(r rune) bool) []byte](#TrimFunc) * [func TrimLeft(s []byte, cutset string) []byte](#TrimLeft) * [func TrimLeftFunc(s []byte, f func(r rune) bool) []byte](#TrimLeftFunc) * [func TrimPrefix(s, prefix []byte) []byte](#TrimPrefix) * [func TrimRight(s []byte, cutset string) []byte](#TrimRight) * [func TrimRightFunc(s []byte, f func(r rune) bool) []byte](#TrimRightFunc) * [func TrimSuffix(s, suffix []byte) []byte](#TrimSuffix) * [func Fields(s []byte) [][]byte](#Fields) * [func FieldsFunc(s []byte, f func(rune) bool) [][]byte](#FieldsFunc) * [func Split(s, sep []byte) [][]byte](#Split) * [func SplitN(s, sep []byte, n int) [][]byte](#SplitN) * [func SplitAfter(s, sep []byte) [][]byte](#SplitAfter) * [func SplitAfterN(s, sep []byte, n int) [][]byte](#SplitAfterN) * [func Join(s [][]byte, sep []byte) []byte](#Join) * [type Reader](#Reader) * [func NewReader(b []byte) \*Reader](#NewReader) * [func (r \*Reader) Len() int](#Reader.Len) * [func (r \*Reader) Read(b []byte) (n int, err error)](#Reader.Read) * [func (r \*Reader) ReadByte() (b byte, err error)](#Reader.ReadByte) * [func (r \*Reader) UnreadByte() error](#Reader.UnreadByte) * [func (r \*Reader) ReadRune() (ch rune, size int, err error)](#Reader.ReadRune) * [func (r \*Reader) UnreadRune() error](#Reader.UnreadRune) * [func (r \*Reader) Seek(offset int64, whence int) (int64, error)](#Reader.Seek) * [func (r \*Reader) ReadAt(b []byte, off int64) (n int, err error)](#Reader.ReadAt) * [func (r \*Reader) WriteTo(w io.Writer) (n int64, err error)](#Reader.WriteTo) * [type Buffer](#Buffer) * [func NewBuffer(buf []byte) \*Buffer](#NewBuffer) * [func NewBufferString(s string) \*Buffer](#NewBufferString) * [func (b \*Buffer) Reset()](#Buffer.Reset) * [func (b \*Buffer) Len() int](#Buffer.Len) * [func (b \*Buffer) Bytes() []byte](#Buffer.Bytes) * [func (b \*Buffer) String() string](#Buffer.String) * [func (b \*Buffer) Truncate(n int)](#Buffer.Truncate) * [func (b \*Buffer) Grow(n int)](#Buffer.Grow) * [func (b \*Buffer) Read(p []byte) (n int, err error)](#Buffer.Read) * [func (b \*Buffer) Next(n int) []byte](#Buffer.Next) * [func (b \*Buffer) ReadByte() (c byte, err error)](#Buffer.ReadByte) * [func (b \*Buffer) UnreadByte() error](#Buffer.UnreadByte) * [func (b \*Buffer) ReadRune() (r rune, size int, err error)](#Buffer.ReadRune) * [func (b \*Buffer) UnreadRune() error](#Buffer.UnreadRune) * [func (b \*Buffer) ReadBytes(delim byte) (line []byte, err error)](#Buffer.ReadBytes) * [func (b \*Buffer) ReadString(delim byte) (line string, err error)](#Buffer.ReadString) * [func (b \*Buffer) Write(p []byte) (n int, err error)](#Buffer.Write) * [func (b \*Buffer) WriteString(s string) (n int, err error)](#Buffer.WriteString) * [func (b \*Buffer) WriteByte(c byte) error](#Buffer.WriteByte) * [func (b \*Buffer) WriteRune(r rune) (n int, err error)](#Buffer.WriteRune) * [func (b \*Buffer) ReadFrom(r io.Reader) (n int64, err error)](#Buffer.ReadFrom) * [func (b \*Buffer) WriteTo(w io.Writer) (n int64, err error)](#Buffer.WriteTo) ### Examples * [Buffer](#example-Buffer) * [Buffer (Reader)](#example-Buffer--Reader) * [Compare](#example-Compare) * [Compare (Search)](#example-Compare--Search) * [TrimPrefix](#example-TrimPrefix) * [TrimSuffix](#example-TrimSuffix) ## Constants ``` const MinRead = 512 ``` MinRead是被Buffer.ReadFrom传递给Read调用的最小尺寸。只要该Buffer在保存内容之外有最少MinRead字节的余量,其ReadFrom方法就不会增加底层的缓冲。 ## Variables ``` var ErrTooLarge = errors.New("bytes.Buffer: too large") ``` 如果内存中不能申请足够保存数据的缓冲,ErrTooLarge就会被传递给panic函数。 ## func [Compare](https://github.com/golang/go/blob/master/src/bytes/bytes_decl.go#L24 "View Source") ``` func Compare(a, b []byte) int ``` Compare函数返回一个整数表示两个[]byte切片按字典序比较的结果(类同C的strcmp)。如果a==b返回0;如果a&lt;b返回-1;否则返回+1。nil参数视为空切片。 Example ``` // Interpret Compare's result by comparing it to zero. var a, b []byte if bytes.Compare(a, b) < 0 { // a less b } if bytes.Compare(a, b) <= 0 { // a less or equal b } if bytes.Compare(a, b) > 0 { // a greater b } if bytes.Compare(a, b) >= 0 { // a greater or equal b } // Prefer Equal to Compare for equality comparisons. if bytes.Equal(a, b) { // a equal b } if !bytes.Equal(a, b) { // a not equal b } ``` Example (Search) ``` // Binary search to find a matching byte slice. var needle []byte var haystack [][]byte // Assume sorted i := sort.Search(len(haystack), func(i int) bool { // Return haystack[i] >= needle. return bytes.Compare(haystack[i], needle) >= 0 }) if i < len(haystack) && bytes.Equal(haystack[i], needle) { // Found it! } ``` ## func [Equal](https://github.com/golang/go/blob/master/src/bytes/bytes_decl.go#L17 "View Source") ``` func Equal(a, b []byte) bool ``` 判断两个切片的内容是否完全相同。 ## func [EqualFold](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L646 "View Source") ``` func EqualFold(s, t []byte) bool ``` 判断两个utf-8编码切片(将unicode大写、小写、标题三种格式字符视为相同)是否相同。 ## func [Runes](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L593 "View Source") ``` func Runes(s []byte) []rune ``` Runes函数返回和s等价的[]rune切片。(将utf-8编码的unicode码值分别写入单个rune) ## func [HasPrefix](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L331 "View Source") ``` func HasPrefix(s, prefix []byte) bool ``` 判断s是否有前缀切片prefix。 ## func [HasSuffix](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L336 "View Source") ``` func HasSuffix(s, suffix []byte) bool ``` 判断s是否有后缀切片suffix。 ## func [Contains](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L81 "View Source") ``` func Contains(b, subslice []byte) bool ``` 判断切片b是否包含子切片subslice。 ## func [Count](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L50 "View Source") ``` func Count(s, sep []byte) int ``` Count计算s中有多少个不重叠的sep子切片。 ## func [Index](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L86 "View Source") ``` func Index(s, sep []byte) int ``` 子切片sep在s中第一次出现的位置,不存在则返回-1。 ## func [IndexByte](https://github.com/golang/go/blob/master/src/bytes/bytes_decl.go#L10 "View Source") ``` func IndexByte(s []byte, c byte) int ``` 字符c在s中第一次出现的位置,不存在则返回-1。 ## func [IndexRune](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L143 "View Source") ``` func IndexRune(s []byte, r rune) int ``` unicode字符r的utf-8编码在s中第一次出现的位置,不存在则返回-1。 ## func [IndexAny](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L158 "View Source") ``` func IndexAny(s []byte, chars string) int ``` 字符串chars中的任一utf-8编码在s中第一次出现的位置,如不存在或者chars为空字符串则返回-1 ## func [IndexFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L510 "View Source") ``` func IndexFunc(s []byte, f func(r rune) bool) int ``` s中第一个满足函数f的位置i(该处的utf-8码值r满足f(r)==true),不存在则返回-1 ## func [LastIndex](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L126 "View Source") ``` func LastIndex(s, sep []byte) int ``` 切片sep在字符串s中最后一次出现的位置,不存在则返回-1。 ## func [LastIndexAny](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L183 "View Source") ``` func LastIndexAny(s []byte, chars string) int ``` 字符串chars中的任一utf-8字符在s中最后一次出现的位置,如不存在或者chars为空字符串则返回-1。 ## func [LastIndexFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L517 "View Source") ``` func LastIndexFunc(s []byte, f func(r rune) bool) int ``` s中最后一个满足函数f的unicode码值的位置i,不存在则返回-1。 ## func [Title](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L443 "View Source") ``` func Title(s []byte) []byte ``` 返回s中每个单词的首字母都改为标题格式的拷贝。 BUG: Title用于划分单词的规则不能很好的处理Unicode标点符号。 ## func [ToLower](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L391 "View Source") ``` func ToLower(s []byte) []byte ``` 返回将所有字母都转为对应的小写版本的拷贝。 ## func [ToLowerSpecial](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L404 "View Source") ``` func ToLowerSpecial(_case unicode.SpecialCase, s []byte) []byte ``` 使用\_case规定的字符映射,返回将所有字母都转为对应的小写版本的拷贝。 ## func [ToUpper](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L388 "View Source") ``` func ToUpper(s []byte) []byte ``` 返回将所有字母都转为对应的大写版本的拷贝。 ## func [ToUpperSpecial](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L398 "View Source") ``` func ToUpperSpecial(_case unicode.SpecialCase, s []byte) []byte ``` 使用\_case规定的字符映射,返回将所有字母都转为对应的大写版本的拷贝。 ## func [ToTitle](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L394 "View Source") ``` func ToTitle(s []byte) []byte ``` 返回将所有字母都转为对应的标题版本的拷贝。 ## func [ToTitleSpecial](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L410 "View Source") ``` func ToTitleSpecial(_case unicode.SpecialCase, s []byte) []byte ``` 使用\_case规定的字符映射,返回将所有字母都转为对应的标题版本的拷贝。 ## func [Repeat](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L378 "View Source") ``` func Repeat(b []byte, count int) []byte ``` 返回count个b串联形成的新的切片。 ## func [Replace](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L608 "View Source") ``` func Replace(s, old, new []byte, n int) []byte ``` 返回将s中前n个不重叠old切片序列都替换为new的新的切片拷贝,如果n&lt;0会替换所有old子切片。 ## func [Map](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L344 "View Source") ``` func Map(mapping func(r rune) rune, s []byte) []byte ``` 将s的每一个unicode码值r都替换为mapping(r),返回这些新码值组成的切片拷贝。如果mapping返回一个负值,将会丢弃该码值而不会被替换(返回值中对应位置将没有码值)。 ## func [Trim](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L570 "View Source") ``` func Trim(s []byte, cutset string) []byte ``` 返回将s前后端所有cutset包含的unicode码值都去掉的子切片。(共用底层数组) ## func [TrimSpace](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L588 "View Source") ``` func TrimSpace(s []byte) []byte ``` 返回将s前后端所有空白(unicode.IsSpace指定)都去掉的子切片。(共用底层数组) ## func [TrimFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L485 "View Source") ``` func TrimFunc(s []byte, f func(r rune) bool) []byte ``` 返回将s前后端所有满足f的unicode码值都去掉的子切片。(共用底层数组) ## func [TrimLeft](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L576 "View Source") ``` func TrimLeft(s []byte, cutset string) []byte ``` 返回将s前端所有cutset包含的unicode码值都去掉的子切片。(共用底层数组) ## func [TrimLeftFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L462 "View Source") ``` func TrimLeftFunc(s []byte, f func(r rune) bool) []byte ``` 返回将s前端所有满足f的unicode码值都去掉的子切片。(共用底层数组) ## func [TrimPrefix](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L491 "View Source") ``` func TrimPrefix(s, prefix []byte) []byte ``` 返回去除s可能的前缀prefix的子切片。(共用底层数组) Example ``` var b = []byte("Goodbye,, world!") b = bytes.TrimPrefix(b, []byte("Goodbye,")) b = bytes.TrimPrefix(b, []byte("See ya,")) fmt.Printf("Hello%s", b) ``` Output: ``` Hello, world! ``` ## func [TrimRight](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L582 "View Source") ``` func TrimRight(s []byte, cutset string) []byte ``` 返回将s后端所有cutset包含的unicode码值都去掉的子切片。(共用底层数组) ## func [TrimRightFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L472 "View Source") ``` func TrimRightFunc(s []byte, f func(r rune) bool) []byte ``` 返回将s后端所有满足f的unicode码值都去掉的子切片。(共用底层数组) ## func [TrimSuffix](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L500 "View Source") ``` func TrimSuffix(s, suffix []byte) []byte ``` 返回去除s可能的后缀suffix的子切片。(共用底层数组) Example ``` var b = []byte("Hello, goodbye, etc!") b = bytes.TrimSuffix(b, []byte("goodbye, etc!")) b = bytes.TrimSuffix(b, []byte("gopher")) b = append(b, bytes.TrimSuffix([]byte("world!"), []byte("x!"))...) os.Stdout.Write(b) ``` Output: ``` Hello, world! ``` ## func [Fields](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L262 "View Source") ``` func Fields(s []byte) [][]byte ``` 返回将字符串按照空白(unicode.IsSpace确定,可以是一到多个连续的空白字符)分割的多个子切片。如果字符串全部是空白或者是空字符串的话,会返回空切片。 ## func [FieldsFunc](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L270 "View Source") ``` func FieldsFunc(s []byte, f func(rune) bool) [][]byte ``` 类似Fields,但使用函数f来确定分割符(满足f的utf-8码值)。如果字符串全部是分隔符或者是空字符串的话,会返回空切片。 ## func [Split](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L250 "View Source") ``` func Split(s, sep []byte) [][]byte ``` 用去掉s中出现的sep的方式进行分割,会分割到结尾,并返回生成的所有[]byte切片组成的切片(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。如果sep为空字符,Split会将s切分成每一个unicode码值一个[]byte切片。 ## func [SplitN](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L233 "View Source") ``` func SplitN(s, sep []byte, n int) [][]byte ``` 用去掉s中出现的sep的方式进行分割,会分割到最多n个子切片,并返回生成的所有[]byte切片组成的切片(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。如果sep为空字符,Split会将s切分成每一个unicode码值一个[]byte切片。参数n决定返回的切片的数目: ``` n > 0 : 返回的切片最多n个子字符串;最后一个子字符串包含未进行切割的部分。 n == 0: 返回nil n < 0 : 返回所有的子字符串组成的切片 ``` ## func [SplitAfter](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L256 "View Source") ``` func SplitAfter(s, sep []byte) [][]byte ``` 用从s中出现的sep后面切断的方式进行分割,会分割到结尾,并返回生成的所有[]byte切片组成的切片(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。如果sep为空字符,Split会将s切分成每一个unicode码值一个[]byte切片。 ## func [SplitAfterN](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L242 "View Source") ``` func SplitAfterN(s, sep []byte, n int) [][]byte ``` 用从s中出现的sep后面切断的方式进行分割,会分割到最多n个子切片,并返回生成的所有[]byte切片组成的切片(每一个sep都会进行一次切割,即使两个sep相邻,也会进行两次切割)。如果sep为空字符,Split会将s切分成每一个unicode码值一个[]byte切片。参数n决定返回的切片的数目: ``` n > 0 : 返回的切片最多n个子字符串;最后一个子字符串包含未进行切割的部分。 n == 0: 返回nil n < 0 : 返回所有的子字符串组成的切片 ``` ## func [Join](https://github.com/golang/go/blob/master/src/bytes/bytes.go#L308 "View Source") ``` func Join(s [][]byte, sep []byte) []byte ``` 将一系列[]byte切片连接为一个[]byte切片,之间用sep来分隔,返回生成的新切片。 ## type [Reader](https://github.com/golang/go/blob/master/src/bytes/reader.go#L17 "View Source") ``` type Reader struct { // 内含隐藏或非导出字段 } ``` Reader类型通过从一个[]byte读取数据,实现了io.Reader、io.Seeker、io.ReaderAt、io.WriterTo、io.ByteScanner、io.RuneScanner接口。 ### func [NewReader](https://github.com/golang/go/blob/master/src/bytes/reader.go#L144 "View Source") ``` func NewReader(b []byte) *Reader ``` NewReader创建一个从s读取数据的Reader。 ### func (\*Reader) [Len](https://github.com/golang/go/blob/master/src/bytes/reader.go#L25 "View Source") ``` func (r *Reader) Len() int ``` Len返回r包含的切片中还没有被读取的部分。 ### func (\*Reader) [Read](https://github.com/golang/go/blob/master/src/bytes/reader.go#L32 "View Source") ``` func (r *Reader) Read(b []byte) (n int, err error) ``` ### func (\*Reader) [ReadByte](https://github.com/golang/go/blob/master/src/bytes/reader.go#L60 "View Source") ``` func (r *Reader) ReadByte() (b byte, err error) ``` ### func (\*Reader) [UnreadByte](https://github.com/golang/go/blob/master/src/bytes/reader.go#L70 "View Source") ``` func (r *Reader) UnreadByte() error ``` ### func (\*Reader) [ReadRune](https://github.com/golang/go/blob/master/src/bytes/reader.go#L79 "View Source") ``` func (r *Reader) ReadRune() (ch rune, size int, err error) ``` ### func (\*Reader) [UnreadRune](https://github.com/golang/go/blob/master/src/bytes/reader.go#L94 "View Source") ``` func (r *Reader) UnreadRune() error ``` ### func (\*Reader) [Seek](https://github.com/golang/go/blob/master/src/bytes/reader.go#L104 "View Source") ``` func (r *Reader) Seek(offset int64, whence int) (int64, error) ``` Seek实现了io.Seeker接口。 ### func (\*Reader) [ReadAt](https://github.com/golang/go/blob/master/src/bytes/reader.go#L45 "View Source") ``` func (r *Reader) ReadAt(b []byte, off int64) (n int, err error) ``` ### func (\*Reader) [WriteTo](https://github.com/golang/go/blob/master/src/bytes/reader.go#L125 "View Source") ``` func (r *Reader) WriteTo(w io.Writer) (n int64, err error) ``` WriteTo实现了io.WriterTo接口。 ## type [Buffer](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L17 "View Source") ``` type Buffer struct { // 内含隐藏或非导出字段 } ``` Buffer是一个实现了读写方法的可变大小的字节缓冲。本类型的零值是一个空的可用于读写的缓冲。 Example ``` var b bytes.Buffer // A Buffer needs no initialization. b.Write([]byte("Hello ")) fmt.Fprintf(&b, "world!") b.WriteTo(os.Stdout) ``` Output: ``` Hello world! ``` Example (Reader) ``` // A Buffer can turn a string or a []byte into an io.Reader. buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==") dec := base64.NewDecoder(base64.StdEncoding, buf) io.Copy(os.Stdout, dec) ``` Output: ``` Gophers rule! ``` ### func [NewBuffer](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L402 "View Source") ``` func NewBuffer(buf []byte) *Buffer ``` NewBuffer使用buf作为初始内容创建并初始化一个Buffer。本函数用于创建一个用于读取已存在数据的buffer;也用于指定用于写入的内部缓冲的大小,此时,buf应为一个具有指定容量但长度为0的切片。buf会被作为返回值的底层缓冲切片。 大多数情况下,new(Buffer)(或只是声明一个Buffer类型变量)就足以初始化一个Buffer了。 ### func [NewBufferString](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L410 "View Source") ``` func NewBufferString(s string) *Buffer ``` NewBuffer使用s作为初始内容创建并初始化一个Buffer。本函数用于创建一个用于读取已存在数据的buffer。 大多数情况下,new(Buffer)(或只是声明一个Buffer类型变量)就足以初始化一个Buffer了。 ### func (\*Buffer) [Reset](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L75 "View Source") ``` func (b *Buffer) Reset() ``` Reset重设缓冲,因此会丢弃全部内容,等价于b.Truncate(0)。 ### func (\*Buffer) [Len](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L57 "View Source") ``` func (b *Buffer) Len() int ``` 返回缓冲中未读取部分的字节长度;b.Len() == len(b.Bytes())。 ### func (\*Buffer) [Bytes](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L43 "View Source") ``` func (b *Buffer) Bytes() []byte ``` 返回未读取部分字节数据的切片,len(b.Bytes()) == b.Len()。如果中间没有调用其他方法,修改返回的切片的内容会直接改变Buffer的内容。 ### func (\*Buffer) [String](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L47 "View Source") ``` func (b *Buffer) String() string ``` 将未读取部分的字节数据作为字符串返回,如果b是nil指针,会返回"&lt;nil&gt;"。 ### func (\*Buffer) [Truncate](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L61 "View Source") ``` func (b *Buffer) Truncate(n int) ``` 丢弃缓冲中除前n字节数据外的其它数据,如果n小于零或者大于缓冲容量将panic。 ### func (\*Buffer) [Grow](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L114 "View Source") ``` func (b *Buffer) Grow(n int) ``` 必要时会增加缓冲的容量,以保证n字节的剩余空间。调用Grow(n)后至少可以向缓冲中写入n字节数据而无需申请内存。如果n小于零或者不能增加容量都会panic。 ### func (\*Buffer) [Read](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L251 "View Source") ``` func (b *Buffer) Read(p []byte) (n int, err error) ``` Read方法从缓冲中读取数据直到缓冲中没有数据或者读取了len(p)字节数据,将读取的数据写入p。返回值n是读取的字节数,除非缓冲中完全没有数据可以读取并写入p,此时返回值err为io.EOF;否则err总是nil。 ### func (\*Buffer) [Next](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L273 "View Source") ``` func (b *Buffer) Next(n int) []byte ``` 返回未读取部分前n字节数据的切片,并且移动读取位置,就像调用了Read方法一样。如果缓冲内数据不足,会返回整个数据的切片。切片只在下一次调用b的读/写方法前才合法。 ### func (\*Buffer) [ReadByte](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L289 "View Source") ``` func (b *Buffer) ReadByte() (c byte, err error) ``` ReadByte读取并返回缓冲中的下一个字节。如果没有数据可用,返回值err为io.EOF。 ### func (\*Buffer) [UnreadByte](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L345 "View Source") ``` func (b *Buffer) UnreadByte() error ``` UnreadByte吐出最近一次读取操作读取的最后一个字节。如果最后一次读取操作之后进行了写入,本方法会返回错误。 ### func (\*Buffer) [ReadRune](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L307 "View Source") ``` func (b *Buffer) ReadRune() (r rune, size int, err error) ``` ReadRune读取并返回缓冲中的下一个utf-8码值。如果没有数据可用,返回值err为io.EOF。如果缓冲中的数据是错误的utf-8编码,本方法会吃掉一字节并返回(U+FFFD, 1, nil)。 ### func (\*Buffer) [UnreadRune](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L330 "View Source") ``` func (b *Buffer) UnreadRune() error ``` UnreadRune吐出最近一次调用ReadRune方法读取的unicode码值。如果最近一次读写操作不是ReadRune,本方法会返回错误。(这里就能看出来UnreadRune比UnreadByte严格多了) ### func (\*Buffer) [ReadBytes](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L362 "View Source") ``` func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) ``` ReadBytes读取直到第一次遇到delim字节,返回一个包含已读取的数据和delim字节的切片。如果ReadBytes方法在读取到delim之前遇到了错误,它会返回在错误之前读取的数据以及该错误(一般是io.EOF)。当且仅当ReadBytes方法返回的切片不以delim结尾时,会返回一个非nil的错误。 ### func (\*Buffer) [ReadString](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L390 "View Source") ``` func (b *Buffer) ReadString(delim byte) (line string, err error) ``` ReadString读取直到第一次遇到delim字节,返回一个包含已读取的数据和delim字节的字符串。如果ReadString方法在读取到delim之前遇到了错误,它会返回在错误之前读取的数据以及该错误(一般是io.EOF)。当且仅当ReadString方法返回的切片不以delim结尾时,会返回一个非nil的错误。 ### func (\*Buffer) [Write](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L125 "View Source") ``` func (b *Buffer) Write(p []byte) (n int, err error) ``` Write将p的内容写入缓冲中,如必要会增加缓冲容量。返回值n为len(p),err总是nil。如果缓冲变得太大,Write会采用错误值ErrTooLarge引发panic。 ### func (\*Buffer) [WriteString](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L134 "View Source") ``` func (b *Buffer) WriteString(s string) (n int, err error) ``` Write将s的内容写入缓冲中,如必要会增加缓冲容量。返回值n为len(p),err总是nil。如果缓冲变得太大,Write会采用错误值ErrTooLarge引发panic。 ### func (\*Buffer) [WriteByte](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L226 "View Source") ``` func (b *Buffer) WriteByte(c byte) error ``` WriteByte将字节c写入缓冲中,如必要会增加缓冲容量。返回值总是nil,但仍保留以匹配bufio.Writer的WriteByte方法。如果缓冲太大,WriteByte会采用错误值ErrTooLarge引发panic。 ### func (\*Buffer) [WriteRune](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L237 "View Source") ``` func (b *Buffer) WriteRune(r rune) (n int, err error) ``` WriteByte将unicode码值r的utf-8编码写入缓冲中,如必要会增加缓冲容量。返回值总是nil,但仍保留以匹配bufio.Writer的WriteRune方法。如果缓冲太大,WriteRune会采用错误值ErrTooLarge引发panic。 ### func (\*Buffer) [ReadFrom](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L150 "View Source") ``` func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) ``` ReadFrom从r中读取数据直到结束并将读取的数据写入缓冲中,如必要会增加缓冲容量。返回值n为从r读取并写入b的字节数;会返回读取时遇到的除了io.EOF之外的错误。如果缓冲太大,ReadFrom会采用错误值ErrTooLarge引发panic。 ### func (\*Buffer) [WriteTo](https://github.com/golang/go/blob/master/src/bytes/buffer.go#L198 "View Source") ``` func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) ``` WriteTo从缓冲中读取数据直到缓冲内没有数据或遇到错误,并将这些数据写入w。返回值n为从b读取并写入w的字节数;返回值总是可以无溢出的写入int类型,但为了匹配io.WriterTo接口设为int64类型。从b读取是遇到的非io.EOF错误及写入w时遇到的错误都会终止本方法并返回该错误。