目录 搜索
archive archive/tar archive/zip bufio bufio(缓存) builtin builtin(内置包) bytes bytes(包字节) compress compress/bzip2(压缩/bzip2) compress/flate(压缩/flate) compress/gzip(压缩/gzip) compress/lzw(压缩/lzw) compress/zlib(压缩/zlib) container container/heap(容器数据结构heap) container/list(容器数据结构list) container/ring(容器数据结构ring) context context(上下文) crypto crypto(加密) crypto/aes(加密/aes) crypto/cipher(加密/cipher) crypto/des(加密/des) crypto/dsa(加密/dsa) crypto/ecdsa(加密/ecdsa) crypto/elliptic(加密/elliptic) crypto/hmac(加密/hmac) crypto/md5(加密/md5) crypto/rand(加密/rand) crypto/rc4(加密/rc4) crypto/rsa(加密/rsa) crypto/sha1(加密/sha1) crypto/sha256(加密/sha256) crypto/sha512(加密/sha512) crypto/subtle(加密/subtle) crypto/tls(加密/tls) crypto/x509(加密/x509) crypto/x509/pkix(加密/x509/pkix) database database/sql(数据库/sql) database/sql/driver(数据库/sql/driver) debug debug/dwarf(调试/dwarf) debug/elf(调试/elf) debug/gosym(调试/gosym) debug/macho(调试/macho) debug/pe(调试/pe) debug/plan9obj(调试/plan9obj) encoding encoding(编码) encoding/ascii85(编码/ascii85) encoding/asn1(编码/asn1) encoding/base32(编码/base32) encoding/base64(编码/base64) encoding/binary(编码/binary) encoding/csv(编码/csv) encoding/gob(编码/gob) encoding/hex(编码/hex) encoding/json(编码/json) encoding/pem(编码/pem) encoding/xml(编码/xml) errors errors(错误) expvar expvar flag flag(命令行参数解析flag包) fmt fmt go go/ast(抽象语法树) go/build go/constant(常量) go/doc(文档) go/format(格式) go/importer go/parser go/printer go/scanner(扫描仪) go/token(令牌) go/types(类型) hash hash(散列) hash/adler32 hash/crc32 hash/crc64 hash/fnv html html html/template(模板) image image(图像) image/color(颜色) image/color/palette(调色板) image/draw(绘图) image/gif image/jpeg image/png index index/suffixarray io io io/ioutil log log log/syslog(日志系统) math math math/big math/big math/bits math/bits math/cmplx math/cmplx math/rand math/rand mime mime mime/multipart(多部分) mime/quotedprintable net net net/http net/http net/http/cgi net/http/cookiejar net/http/fcgi net/http/httptest net/http/httptrace net/http/httputil net/http/internal net/http/pprof net/mail net/mail net/rpc net/rpc net/rpc/jsonrpc net/smtp net/smtp net/textproto net/textproto net/url net/url os os os/exec os/signal os/user path path path/filepath(文件路径) plugin plugin(插件) reflect reflect(反射) regexp regexp(正则表达式) regexp/syntax runtime runtime(运行时) runtime/debug(调试) runtime/internal/sys runtime/pprof runtime/race(竞争) runtime/trace(执行追踪器) sort sort(排序算法) strconv strconv(转换) strings strings(字符串) sync sync(同步) sync/atomic(原子操作) syscall syscall(系统调用) testing testing(测试) testing/iotest testing/quick text text/scanner(扫描文本) text/tabwriter text/template(定义模板) text/template/parse time time(时间戳) unicode unicode unicode/utf16 unicode/utf8 unsafe unsafe
文字

  • 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.Grow

Buffer (Reader)

Compare

Compare (Search)

Contains

Count

EqualFold

Fields

FieldsFunc

HasPrefix

HasSuffix

Index

IndexAny

IndexFunc

IndexRune

Join

LastIndex

Map

Repeat

Replace

Split

SplitAfter

SplitAfterN

SplitN

Title

ToLower

ToTitle

ToUpper

Trim

TrimPrefix

TrimSpace

TrimSuffix

包文件

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 mainimport ("bytes")func main() {// 通过将比较结果与零比较来解释比较结果。var a, b []byteif 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 mainimport ("bytes""sort")func main() {// 二进制搜索查找匹配的字节片。var needle []bytevar 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 mainimport ("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 mainimport ("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 mainimport ("bytes""fmt")func main() {
	fmt.Println(bytes.EqualFold([]byte("Go"), []byte("go")))}

func Fields

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

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

示例

package mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("bytes""fmt")func main() {
	rot13 := func(r rune) rune {switch {case r >= 'A' && r <= 'Z':return 'A' + (r-'A'+13)%26case 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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("bytes""fmt")func main() {
	fmt.Printf("%s", bytes.Title([]byte("her royal highness")))}

func ToLower

func ToLower(s []byte) []byte

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

示例

package mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("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 mainimport ("bytes""fmt""os")func main() {var b bytes.Buffer // 缓冲区不需要初始化。
	b.Write([]byte("Hello "))
	fmt.Fprintf(&b, "world!")
	b.WriteTo(os.Stdout)}

示例(Reader)

package mainimport ("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 mainimport ("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标点符号。

上一篇: 下一篇: