字节 | bytes

Package bytes

  • import "bytes"

  • 概述

  • 索引

  • 示例

概述

包字节实现了操作字节片的函数。它类似于字符串包的设施。

索引

Constants(常量)

Variables(变量)

func Compare(a, b []byte) int

func Contains(b, subslice []byte) bool

func ContainsAny(b []byte, chars string) bool

func ContainsRune(b []byte, r rune) bool

func Count(s, sep []byte) int

func Equal(a, b []byte) bool

func EqualFold(s, t []byte) bool

func Fields(s []byte) [][]byte

func FieldsFunc(s []byte, f func(rune) bool) [][]byte

func HasPrefix(s, prefix []byte) bool

func HasSuffix(s, suffix []byte) bool

func Index(s, sep []byte) int

func IndexAny(s []byte, chars string) int

func IndexByte(s []byte, c byte) int

func IndexFunc(s []byte, f func(r rune) bool) int

func IndexRune(s []byte, r rune) int

func Join(s [][]byte, sep []byte) []byte

func LastIndex(s, sep []byte) int

func LastIndexAny(s []byte, chars string) int

func LastIndexByte(s []byte, c byte) int

func LastIndexFunc(s []byte, f func(r rune) bool) int

func Map(mapping func(r rune) rune, s []byte) []byte

func Repeat(b []byte, count int) []byte

func Replace(s, old, new []byte, n int) []byte

func Runes(s []byte) []rune

func Split(s, sep []byte) [][]byte

func SplitAfter(s, sep []byte) [][]byte

func SplitAfterN(s, sep []byte, n int) [][]byte

func SplitN(s, sep []byte, n int) [][]byte

func Title(s []byte) []byte

func ToLower(s []byte) []byte

func ToLowerSpecial(c unicode.SpecialCase, s []byte) []byte

func ToTitle(s []byte) []byte

func ToTitleSpecial(c unicode.SpecialCase, s []byte) []byte

func ToUpper(s []byte) []byte

func ToUpperSpecial(c unicode.SpecialCase, s []byte) []byte

func Trim(s []byte, cutset string) []byte

func TrimFunc(s []byte, f func(r rune) bool) []byte

func TrimLeft(s []byte, cutset string) []byte

func TrimLeftFunc(s []byte, f func(r rune) bool) []byte

func TrimPrefix(s, prefix []byte) []byte

func TrimRight(s []byte, cutset string) []byte

func TrimRightFunc(s []byte, f func(r rune) bool) []byte

func TrimSpace(s []byte) []byte

func TrimSuffix(s, suffix []byte) []byte

type Buffer

  • func NewBuffer(buf []byte) *Buffer

  • func NewBufferString(s string) *Buffer

  • func (b *Buffer) Bytes() []byte

  • func (b *Buffer) Cap() int

  • func (b *Buffer) Grow(n int)

  • func (b *Buffer) Len() int

  • func (b *Buffer) Next(n int) []byte

  • func (b *Buffer) Read(p []byte) (n int, err error)

  • func (b *Buffer) ReadByte() (byte, error)

  • func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)

  • func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)

  • func (b *Buffer) ReadRune() (r rune, size int, err error)

  • func (b *Buffer) ReadString(delim byte) (line string, err error)

  • func (b *Buffer) Reset()

  • func (b *Buffer) String() string

  • func (b *Buffer) Truncate(n int)

  • func (b *Buffer) UnreadByte() error

  • func (b *Buffer) UnreadRune() error

  • func (b *Buffer) Write(p []byte) (n int, err error)

  • func (b *Buffer) WriteByte(c byte) error

  • func (b *Buffer) WriteRune(r rune) (n int, err error)

  • func (b *Buffer) WriteString(s string) (n int, err error)

  • func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)

type Reader

  • func NewReader(b []byte) *Reader

  • func (r *Reader) Len() int

  • func (r *Reader) Read(b []byte) (n int, err error)

  • func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)

  • func (r *Reader) ReadByte() (byte, error)

  • func (r *Reader) ReadRune() (ch rune, size int, err error)

  • func (r *Reader) Reset(b []byte)

  • func (r *Reader) Seek(offset int64, whence int) (int64, error)

  • func (r *Reader) Size() int64

  • func (r *Reader) UnreadByte() error

  • func (r *Reader) UnreadRune() error

  • func (r *Reader) WriteTo(w io.Writer) (n int64, err error)

Bugs

示例

Buffer

包文件

buffer.go bytes.go bytes_amd64.go bytes_decl.go reader.go

常量

MinRead是由Buffer.ReadFrom传递给Read调用的最小切片大小。只要缓冲区的最小读取字节数超过了保存r内容所需的字节数,ReadFrom将不会增长底层缓冲区。

const MinRead = 512

变量

如果无法分配内存以将数据存储在缓冲区中,则 ErrTooLarge 会传递给panic。

var ErrTooLarge = errors.New("bytes.Buffer: too large")

func Compare(查看源代码)

func Compare(a, b []byte) int

Compare 按字典顺序返回一个比较两个字节片段的整数。如果a == b,结果将为0,如果 a <b 则返回-1,如果 a> b 则返回+1。零参数相当于一个空片。

示例

package main import ( "bytes" ) func main() { // 通过将比较结果与零比较来解释比较结果。 var a, b []byte if bytes.Compare(a, b) < 0 { // a 小于 b } if bytes.Compare(a, b) <= 0 { // a 小于或等于 b } if bytes.Compare(a, b) > 0 { // a 大于 b } if bytes.Compare(a, b) >= 0 { // a 大于或等于 b } // 为了比较平等,优先选择等于比较。 if bytes.Equal(a, b) { // a 等于 b } if !bytes.Equal(a, b) { // a 不等于 b } }

示例(搜索)

package main import ( "bytes" "sort" ) func main() { // 二进制搜索查找匹配的字节片。 var needle []byte var haystack [][]byte // 假设排序 i := sort.Search(len(haystack), func(i int) bool { // 返回 haystack[i] >= needle。 return bytes.Compare(haystack[i], needle) >= 0 }) if i < len(haystack) && bytes.Equal(haystack[i], needle) { // 找到了 } }

func Contains(查看源代码)

func Contains(b, subslice []byte) bool

包含报告 sublice 是否在 b 之内。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Println(bytes.Contains([]byte("seafood"), []byte("foo"))) fmt.Println(bytes.Contains([]byte("seafood"), []byte("bar"))) fmt.Println(bytes.Contains([]byte("seafood"), []byte(""))) fmt.Println(bytes.Contains([]byte(""), []byte(""))) }

func ContainsAny(查看源代码)

func ContainsAny(b []byte, chars string) bool

ContainsAny 报告字符中的任何 UTF-8 编码的 Unicode 代码点是否在 b 中。

func ContainsRune(查看源代码)

func ContainsRune(b []byte, r rune) bool

ContainsRune 报告 Unicode 代码点 r 是否在 b 之内。

func Count(查看源代码)

func Count(s, sep []byte) int

Count 计算s中不重叠实例的数量。如果 sep 为空片段,则 Count 返回1 + s中的 Unicode 代码点数。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Println(bytes.Count([]byte("cheese"), []byte("e"))) fmt.Println(bytes.Count([]byte("five"), []byte(""))) // before & after each rune }

func Equal(查看源代码)

func Equal(a, b []byte) bool

Equal 返回一个布尔值,报告 a 和 b 是否是相同的长度并且包含相同的字节。零参数相当于一个空片。

fuc EqualFold(查看源代码)

func EqualFold(s, t []byte) bool

EqualFold 报告无论 s 和 t,解释为 UTF-8 字符串,在 Unicode 大小写折叠下是否相等。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go"))) }

func Fields(查看源代码)

func Fields(s []byte) [][]byte

字段在一个或多个连续空白字符的每个实例周围分割切片,如果 s 仅包含空格,则返回 s 的子片段或空列表。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("Fields are: %q", bytes.Fields([]byte(" foo bar baz "))) }

func FieldsFunc(查看源文档)

func FieldsFunc(s []byte, f func(rune) bool) [][]byte

FieldsFunc 将s解释为 UTF-8 编码的 Unicode 代码点序列。它在每次满足 f(c) 的代码点 c 运行时分割片 s 并返回s的一个子片段。如果s中的所有代码点满足 f(c) 或len(s) == 0,则返回空片。FieldsFunc 不保证它调用f(c)的顺序。如果f没有为给定的 c 返回一致的结果,那么 FieldsFunc 可能会崩溃。

示例

package main import ( "bytes" "fmt" "unicode" ) func main() { f := func(c rune) bool { return !unicode.IsLetter(c) && !unicode.IsNumber(c) } fmt.Printf("Fields are: %q", bytes.FieldsFunc([]byte(" foo1;bar2,baz3..."), f)) }

func HasPrefix(查看源代码)

func HasPrefix(s, prefix []byte) bool

HasPrefix测试字节片是否以前缀开头。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("Go"))) fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte("C"))) fmt.Println(bytes.HasPrefix([]byte("Gopher"), []byte(""))) }

func HasSuffix(查看源代码)

func HasSuffix(s, suffix []byte) bool

HasSuffix 测试字节片段是否以后缀结尾。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("go"))) fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("O"))) fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte("Ami"))) fmt.Println(bytes.HasSuffix([]byte("Amigo"), []byte(""))) }

func Index(查看源代码)

func Index(s, sep []byte) int

索引返回 s 中第一个 sep 实例的索引,如果 s 中不存在 sep,则返回-1。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Println(bytes.Index([]byte("chicken"), []byte("ken"))) fmt.Println(bytes.Index([]byte("chicken"), []byte("dmr"))) }

func IndexAny(查看源代码)

func IndexAny(s []byte, chars string) int

IndexAny 将 s 解释为 UTF-8 编码的 Unicode 代码点序列。它返回字符中任何 Unicode 代码点的 s 中第一次出现的字节索引。如果字符为空或者没有共同的代码点,则返回-1。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Println(bytes.IndexAny([]byte("chicken"), "aeiouy")) fmt.Println(bytes.IndexAny([]byte("crwth"), "aeiouy")) }

func IndexByte(查看源代码)

func IndexByte(s []byte, c byte) int

IndexByte 返回 s 的第一个实例的索引,如果 c 不存在于 s 中,则返回 -1。

func IndexFunc(查看源代码)

func IndexFunc(s []byte, f func(r rune) bool) int

IndexFunc 将 s 解释为一系列UTF-8编码的Unicode代码点。它返回满足 f(c) 的第一个 Unicode 代码点的 s 中的字节索引,否则返回 -1。

示例

package main import ( "bytes" "fmt" "unicode" ) func main() { f := func(c rune) bool { return unicode.Is(unicode.Han, c) } fmt.Println(bytes.IndexFunc([]byte("Hello, 世界"), f)) fmt.Println(bytes.IndexFunc([]byte("Hello, world"), f)) }

func IndexRune(查看源代码)

func IndexRune(s []byte, r rune) int

IndexRune将 s 解释为一系列UTF-8编码的Unicode代码点。它返回给定符文的 s 中第一个出现的字节索引。如果符文不在 s 中,它会返回-1。如果 r 是utf8.RuneError,它将返回任何无效的UTF-8字节序列的第一个实例。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Println(bytes.IndexRune([]byte("chicken"), 'k')) fmt.Println(bytes.IndexRune([]byte("chicken"), 'd')) }

func Join(查看源代码)

func Join(s [][]byte, sep []byte) []byte

Join 连接 s的元素以创建一个新的字节片。分隔符 sep 放置在生成的切片中的元素之间。

示例

package main import ( "bytes" "fmt" ) func main() { s := [][]byte{[]byte("foo"), []byte("bar"), []byte("baz")} fmt.Printf("%s", bytes.Join(s, []byte(", "))) }

func LastIndex(查看源代码)

func LastIndex(s, sep []byte) int

LastIndex 返回 s 中最后一个 sep 实例的索引,如果 sep 中不存在 s,则返回-1。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Println(bytes.Index([]byte("go gopher"), []byte("go"))) fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("go"))) fmt.Println(bytes.LastIndex([]byte("go gopher"), []byte("rodent"))) }

func LastIndexAny(查看源代码)

func LastIndexAny(s []byte, chars string) int

LastIndexAny 将 s 解释为UTF-8编码的 Unicode 代码点序列。它返回字符中任何 Unicode 代码点的最后一次出现的字节索引。如果字符为空或者没有共同的代码点,则返回-1。

func LastIndexByte(查看源代码)

func LastIndexByte(s []byte, c byte) int

LastIndexByte 返回 s 的最后一个实例的索引,如果 c 不存在于 s 中,则返回-1。

func LastIndexFunc(查看源代码)

func LastIndexFunc(s []byte, f func(r rune) bool) int

LastIndexFunc 将 s 解释为UTF-8编码的 Unicode 代码点序列。它返回满足 f(c) 的最后一个 Unicode 代码点的 s 中的字节索引,否则返回-1。

func Map(查看源代码)

func Map(mapping func(r rune) rune, s []byte) []byte

Map 根据映射函数返回字节切片s的所有字符修改后的副本。如果映射返回负值,则字符将从字符串中删除而不会被替换。 s 和输出中的字符被解释为 UTF-8 编码的 Unicode 代码点。

示例

package main import ( "bytes" "fmt" ) func main() { rot13 := func(r rune) rune { switch { case r >= 'A' && r <= 'Z': return 'A' + (r-'A'+13)%26 case r >= 'a' && r <= 'z': return 'a' + (r-'a'+13)%26 } return r } fmt.Printf("%s", bytes.Map(rot13, []byte("'Twas brillig and the slithy gopher..."))) }

func Repeat(查看源代码)

func Repeat(b []byte, count int) []byte

重复返回由 b 的计数副本组成的新字节片段。

如果 count 为负数或者 (len(b) * count) 的结果溢出,它会发生混乱。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("ba%s", bytes.Repeat([]byte("na"), 2)) }

func Replace(查看源代码)

func Replace(s, old, new []byte, n int) []byte

Replace 将返回 slice 的一个副本,其中前 n 个非重叠的旧实例将被 new 替换。如果 old 是空的,它会在切片的开头和每个 UTF-8 序列之后进行匹配,最多可产生 k-1 切片的 k+1 替换。如果 n<0,则替换次数没有限制。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2)) fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1)) }

func Runes(查看源代码)

func Runes(s []byte) []rune

符文返回相当于 s 的一段符文(Unicode 代码点)。

func Split(查看源代码)

func Split(s, sep []byte) [][]byte

将切片分割成由 sep 分隔的所有子切片,并返回这些分隔符之间的一部分子切片。如果 sep 为空,Split会在每个UTF-8序列之后分裂。它相当于 SplitN,计数为 -1 。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("%q\n", bytes.Split([]byte("a,b,c"), []byte(","))) fmt.Printf("%q\n", bytes.Split([]byte("a man a plan a canal panama"), []byte("a "))) fmt.Printf("%q\n", bytes.Split([]byte(" xyz "), []byte(""))) fmt.Printf("%q\n", bytes.Split([]byte(""), []byte("Bernardo O'Higgins"))) }

func SplitAfter(查看源代码)

func SplitAfter(s, sep []byte) [][]byte

SplitAfter 在 sep 的每个实例之后切片到所有 sublices 中,并返回这些 sublices 的一部分。如果 sep 为空,则 SplitAfter 会在每个 UTF-8 序列后分割。它相当于 SplitAfterN ,计数为 -1 。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("%q\n", bytes.SplitAfter([]byte("a,b,c"), []byte(","))) }

func SplitAfterN(查看源代码)

func SplitAfterN(s, sep []byte, n int) [][]byte

在每个 sep 实例之后,SplitAfterN 将 s 分割成子项,并返回这些子项的一部分。如果 sep 为空,则 SplitAfterN 在每个 UTF-8 序列之后分割。计数确定要返回的子备份数量:

n > 0: 至多n个子模板;最后一个子切片将成为未分割的剩余部分。 n == 0: the result is nil (zero subslices) n < 0: 所有sublices

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("%q\n", bytes.SplitAfterN([]byte("a,b,c"), []byte(","), 2)) }

func SplitN(查看源代码)

func SplitN(s, sep []byte, n int) [][]byte

将 SplitN 切片成由 sep 分隔的子片段,并返回这些分隔片之间的一部分子片段。如果 sep 为空, SplitN 会在每个UTF-8序列之后分裂。计数确定要返回的子备份数量:

n > 0: at most n subslices; the last subslice will be the unsplit remainder. n == 0: the result is nil (zero subslices) n < 0: all subslices

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("%q\n", bytes.SplitN([]byte("a,b,c"), []byte(","), 2)) z := bytes.SplitN([]byte("a,b,c"), []byte(","), 0) fmt.Printf("%q (nil = %v)\n", z, z == nil) }

func Title(查看源代码)

func Title(s []byte) []byte

标题返回一个 s 的副本,其中包含所有 Unicode 字母,这些字母开始被映射到其标题大小写。

BUG(rsc):规则标题用于单词边界的规则不能正确处理 Unicode 标点符号。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("%s", bytes.Title([]byte("her royal highness"))) }

func ToLower(查看源代码)

func ToLower(s []byte) []byte

ToLower 返回所有 Unicode 字母映射为小写字节片段的副本。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("%s", bytes.ToLower([]byte("Gopher"))) }

func ToLowerSpecial(查看源代码)

func ToLowerSpecial(c unicode.SpecialCase, s []byte) []byte

ToLowerSpecial 返回所有 Unicode 字母映射到小写字节的字节切片的副本,优先考虑特殊的外壳规则。

func ToTitle(查看源代码)

func ToTitle(s []byte) []byte

ToTitle 返回字节切片 s 的副本,并将所有 Unicode 字母映射到它们的标题大小写。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("%s\n", bytes.ToTitle([]byte("loud noises"))) fmt.Printf("%s\n", bytes.ToTitle([]byte("хлеб"))) }

func ToTitleSpecial(查看源代码)

func ToTitleSpecial(c unicode.SpecialCase, s []byte) []byte

ToTitleSpecial 返回字节片段 s 的副本,并将所有 Unicode 字母映射到它们的标题大小写,优先考虑特殊外壳规则。

func ToUpper(查看源代码)

func ToUpper(s []byte) []byte

ToUpper 返回字节切片 s 的副本,并将所有 Unicode 字母映射为大写字母。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("%s", bytes.ToUpper([]byte("Gopher"))) }

func ToUpperSpecial(查看源代码)

func ToUpperSpecial(c unicode.SpecialCase, s []byte) []byte

ToUpperSpecial 返回字节切片 s 的副本,其中所有 Unicode 字母都映射为大写字母,优先考虑特殊外壳规则。

func Trim(查看源代码)

func Trim(s []byte, cutset string) []byte

Trim 通过切割 cutset 中包含的所有前导和尾随 UTF-8 编码的 Unicode 代码点来返回 s 的子片段。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("[%q]", bytes.Trim([]byte(" !!! Achtung! Achtung! !!! "), "! ")) }

func TrimFunc(查看源代码)

func TrimFunc(s []byte, f func(r rune) bool) []byte

TrimFunc 通过分割所有满足f(c)的前导和尾随 UTF-8 编码的 Unicode 代码点 c 来返回 s 的子片段。

func TrimLeft(查看源代码)

func TrimLeft(s []byte, cutset string) []byte

TrimLeft 通过切割 cutset 中包含的所有主要UTF-8编码的 Unicode 代码点来返回 s 的子片段。

func TrimLeftFunc(查看源代码)

func TrimLeftFunc(s []byte, f func(r rune) bool) []byte

TrimLeftFunc 通过分割所有满足 f(c) 的 UTF-8 编码的 Unicode 编码点 c 来返回 s 的子片段。

func TrimPrefix(查看源代码)

func TrimPrefix(s, prefix []byte) []byte

TrimPrefix 在没有提供的前导前缀字符串的情况下返回 s 。如果 s 不是以前缀开始,则 s 不变。

示例

package main import ( "bytes" "fmt" ) func main() { var b = []byte("Goodbye,, world!") b = bytes.TrimPrefix(b, []byte("Goodbye,")) b = bytes.TrimPrefix(b, []byte("See ya,")) fmt.Printf("Hello%s", b) }

func TrimRight(查看源代码)

func TrimRight(s []byte, cutset string) []byte

TrimRight 通过切割 cutset 中包含的所有尾随 UTF-8 编码的 Unicode 代码点来返回 s 的子片段。

func TrimRightFunc(查看源代码)

func TrimRightFunc(s []byte, f func(r rune) bool) []byte

TrimRightFunc 通过分割所有满足 f(c) 的 UTF-8 编码的 Unicode 代码点 c 来返回 s 的子片段。

func TrimSpace(查看源代码)

func TrimSpace(s []byte) []byte

TrimSpace 根据 Unicode 定义的所有前导和尾随空白区域来返回 s 的子片段。

示例

package main import ( "bytes" "fmt" ) func main() { fmt.Printf("%s", bytes.TrimSpace([]byte(" \t\n a lone gopher \n\t\r\n"))) }

func TrimSuffix(查看源代码)

func TrimSuffix(s, suffix []byte) []byte

TrimSuffix 在没有提供尾随后缀字符串的情况下返回 s 。如果 s 不以后缀结尾,则 s 不变。

示例

package main import ( "bytes" "os" ) func main() { 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) }

type Buffer(查看源代码)

缓冲区是一个可变大小的带有读和写方法的字节缓冲区。Buffer 的零值是一个准备使用的空缓冲区。

type Buffer struct { // 包含过滤或未导出的字段 }

示例

package main import ( "bytes" "fmt" "os" ) func main() { var b bytes.Buffer // 缓冲区不需要初始化。 b.Write([]byte("Hello ")) fmt.Fprintf(&b, "world!") b.WriteTo(os.Stdout) }

示例(Reader)

package main import ( "bytes" "encoding/base64" "io" "os" ) func main() { // 缓冲区可以将字符串或[]字节转换为io.Reader。 buf := bytes.NewBufferString("R29waGVycyBydWxlIQ==") dec := base64.NewDecoder(base64.StdEncoding, buf) io.Copy(os.Stdout, dec) }

func NewBuffer(查看源代码)

func NewBuffer(buf []byte) *Buffer

NewBuffer 使用 buf 作为其初始内容创建并初始化新的 Buffer。新的缓冲区取得了 buf 的所有权,并且调用者在调用之后不应该使用 buf 。NewBuffer 旨在准备一个缓冲区来读取现有数据。它也可以用来调整写入的内部缓冲区大小。要做到这一点,buf 应该具有所需的容量,但长度为零。

在大多数情况下,新的(缓冲区)(或者只是声明一个缓冲区变量)足以初始化缓冲区。

func NewBufferString(查看源代码)

func NewBufferString(s string) *Buffer

NewBufferString 使用字符串 s 作为其初始内容创建并初始化一个新的 Buffer。它旨在准备一个缓冲区来读取现有的字符串。

在大多数情况下,新的(缓冲区)(或者只是声明一个缓冲区变量)足以初始化缓冲区。

func (*Buffer) Bytes(查看源代码)

func (b *Buffer) Bytes() []byte

字节返回一段长度 b.Len(),其中包含缓冲区的未读部分。该切片仅在下一次缓冲区修改之前有效(即,直到下一次调用 Read,Write,Reset或Truncate 之类的方法)。至少在下一次缓冲区修改之前,切片会对缓冲区内容进行别名,因此切片的即时更改将影响将来读取的结果。

func (*Buffer) Cap(查看源代码)

func (b *Buffer) Cap() int

Cap 返回缓冲区底层字节片段的容量,即分配给缓冲区数据的总空间。

func (*Buffer) Grow(查看源代码)

func (b *Buffer) Grow(n int)

如果有必要,增长缓冲区的容量以保证另外n个字节的空间。在Grow(n)之后,至少可以将n个字节写入缓冲区而无需其他分配。如果n是负数,Grow 会陷入混乱。如果缓冲区不能增长,它会与 ErrTooLarge 一起发生混乱。

示例

package main import ( "bytes" "fmt" ) func main() { var b bytes.Buffer b.Grow(64) bb := b.Bytes() b.Write([]byte("64 bytes or fewer")) fmt.Printf("%q", bb[:b.Len()]) }

func (*Buffer) Len(查看源代码)

func (b *Buffer) Len() int

Len 返回缓冲区未读部分的字节数;b.Len() == len(b.Bytes())。

func (*Buffer) Next(查看源代码)

func (b *Buffer) Next(n int) []byte

接下来返回包含来自缓冲区的下n个字节的切片,如同字节已由 Read 返回一样推进缓冲区。如果缓冲区中少于n个字节,则 Next 返回整个缓冲区。切片只有在下次调用读取或写入方法时才有效。

func (*Buffer) Read(查看源代码)

func (b *Buffer) Read(p []byte) (n int, err error)

Read 从缓冲区中读取下一个 len(p) 字节,或者直到缓冲区被耗尽。返回值n是读取的字节数。如果缓冲区没有数据要返回,则 err 为 io.EOF(除非len(p)为零); 否则为零。

func (*Buffer) ReadByte(查看源代码)

func (b *Buffer) ReadByte() (byte, error)

ReadByte 读取并返回缓冲区中的下一个字节。如果没有可用的字节,则返回错误 io.EOF。

func (*Buffer) ReadBytes(查看源代码)

func (b *Buffer) ReadBytes(delim byte) (line []byte, err error)

ReadBytes 一直读取,直到输入中delim第一次出现时停止 ,并返回一个包含小于等于分隔符数据的片段。如果ReadBytes在查找分隔符之前遇到错误,它将返回错误之前读取的数据和错误本身(通常为 io.EOF )。当且仅当返回的数据不以分隔符结束时,ReadBytes 才返回 err != nil。

func (*Buffer) ReadFrom(查看源代码)

func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom 从 r 读取数据直到 EOF 并将其附加到缓冲区,根据需要生长缓冲区。返回值n是读取的字节数。读取过程中遇到的除 io.EOF 外的任何错误也会返回。如果缓冲区变得过大,ReadFrom 将与 ErrTooLarge 一起发生混乱。

func (*Buffer) ReadRune(查看源代码)

func (b *Buffer) ReadRune() (r rune, size int, err error)

ReadRune 读取并返回缓冲区中的下一个UTF-8编码的 Unicode 代码点。如果没有字节可用,返回的错误是io.EOF。如果字节是错误的UTF-8编码,则它将消耗一个字节并返回U + FFFD,1。

func (*Buffer) ReadString(查看源代码)

func (b *Buffer) ReadString(delim byte) (line string, err error)

ReadString 读取,直到输入中第一次出现 delim ,返回一个包含数据的字符串直到并包含分隔符。如果ReadString 在查找分隔符之前遇到错误,它将返回在错误之前读取的数据和错误本身(通常为 io.EOF )。当且仅当返回的数据没有以分隔符结束时,ReadString 返回 err!= nil。

func (*Buffer) Reset(查看源代码)

func (b *Buffer) Reset()

Reset将缓冲区重置为空,但它保留了未来写入使用的底层存储。重置与Truncate(0) 相同。

func (*Buffer) String(查看源代码)

func (b *Buffer) String() string

字符串以字符串形式返回缓冲区未读部分的内容。如果 Buffer 是一个零指针,它将返回“<nil>”。

func (*Buffer) Truncate(查看源代码)

func (b *Buffer) Truncate(n int)

截断从缓冲区丢弃除前 n 个未读字节以外的所有字节,但继续使用相同的分配存储。如果 n 是负数或大于缓冲区的长度,它会发生混乱。

func (*Buffer) UnreadByte(查看源代码)

func (b *Buffer) UnreadByte() error

UnreadByte 读取最近成功读取操作返回的最后一个字节,该操作至少读取一个字节。如果自上次读取后发生写入,如果上次读取返回错误,或者读取读取的是零字节,则 UnreadByte 返回错误。

func (*Buffer) UnreadRune(查看源代码)

func (b *Buffer) UnreadRune() error

UnreadRune 未读取 ReadRune 返回的最后一个符文。如果缓冲区上的最新读取或写入操作不是成功的ReadRune,则 UnreadRune 将返回错误。(在这方面,它比 UnreadByte 更严格,它将读取任何读操作的最后一个字节。)

func (*Buffer) Write(查看源代码)

func (b *Buffer) Write(p []byte) (n int, err error)

写入将 p 的内容附加到缓冲区,根据需要增加缓冲区。返回值n是 p 的长度; err 总是零。如果缓冲区变得过大,Write 会与 ErrTooLarge 混淆。

func (*Buffer) WriteByte(查看源代码)

func (b *Buffer) WriteByte(c byte) error

WriteByte 将字节 c 附加到缓冲区,根据需要生长缓冲区。返回的错误始终为零,但包含在内以匹配 bufio.Writer 的 WriteByte 。如果缓冲区变得太大,WriteByte 会与 ErrTooLarge 一起发生混乱。

func (*Buffer) WriteRune(查看源代码)

func (b *Buffer) WriteRune(r rune) (n int, err error)

WriteRune 将 Unicode 代码点 r 的UTF-8 编码附加到缓冲区,返回其长度和错误,该错误总是为零,但包含在内以匹配 bufio.Writer 的 WriteRune。缓冲区根据需要增长;如果它变得太大,WriteRune 将会与 ErrTooLarge 混淆。

func (*Buffer) WriteString(查看源代码)

func (b *Buffer) WriteString(s string) (n int, err error)

WriteString将 s 的内容附加到缓冲区,根据需要增加缓冲区。返回值 n 是 s 的长度;err 总是零。如果缓冲区变得太大,WriteString 会与 ErrTooLarge 混淆。

func (*Buffer) WriteTo(查看源代码)

func (b *Buffer) WriteTo(w io.Writer) (n int64, err error)

WriteTo 将数据写入 w 直到缓冲区耗尽或发生错误。返回值n是写入的字节数;它总是适合 int ,但它是 int64 来匹配 io.WriterTo 接口。写入过程中遇到的任何错误也会返回。

type Reader(查看源代码)

Reader 通过从字节片段读取来实现 io.Reader,io.ReaderAt,io.WriterTo,io.Seeker,io.ByteScanner和io.RuneScanner 接口。与缓冲区不同,Reader 是只读的并支持搜索。

type Reader struct { // 包含过滤或未导出的字段 }

func NewReader(查看源代码)

func NewReader(b []byte) *Reader

NewReader从 b 返回新的 Reader 读数。

func (*Reader) Len(查看源代码)

func (r *Reader) Len() int

Len 返回切片的未读部分的字节数。

func (*Reader) Read(查看源代码)

func (r *Reader) Read(b []byte) (n int, err error)

func (*Reader) ReadAt(查看源代码)

func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)

func (*Reader) ReadByte(查看源代码)

func (r *Reader) ReadByte() (byte, error)

func (*Reader) ReadRune(查看源代码)

func (r *Reader) ReadRune() (ch rune, size int, err error)

func (*Reader) Reset(查看源代码)

func (r *Reader) Reset(b []byte)

Reset 将Reader(读取器)重置为从 b 中读取。

func (*Reader) Seek(查看源代码)

func (r *Reader) Seek(offset int64, whence int) (int64, error)

Seek 实现了 io.Seeker 接口。

func (*Reader) Size(查看源代码)

func (r *Reader) Size() int64

Size 返回基础字节片的原始长度。大小是可通过 ReadAt 读取的字节数。返回的值总是相同的,不受调用任何其他方法的影响。

func (*Reader) UnreadByte(查看源代码)

func (r *Reader) UnreadByte() error

func (*Reader) UnreadRune(查看源代码)

func (r *Reader) UnreadRune() error

func (*Reader) WriteTo(查看源代码)

func (r *Reader) WriteTo(w io.Writer) (n int64, err error)

WriteTo 实现 io.WriterTo 接口。

错误

  • 标题用于单词边界的规则不能正确处理Unicode标点符号。