本次主要介绍golang中的标准库bytes
,基本上参考了 字节 | bytes 、Golang标准库——bytes 文章。
bytes
库主要包含 5 大部分,即:
- 常量
- 变量
- 函数
- Buffer
- Reader
我们依次学习上面的 5 大部分。
1、常量
const MinRead = 512
bytes.MinRead 是一个常量,表示在使用 ReadFrom 方法从 io.Reader 中读取数据时,每次读取的最小字节数。如果 io.Reader 的 Read 方法返回的字节数小于 bytes.MinRead,ReadFrom 方法会尝试再次读取,直到读取的字节数达到 bytes.MinRead 或者 io.EOF。这个常量的值为 512。
对上面解释不太清楚的同学,可以去看看源码,然后再看一看
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)
这个方法就能够理解了。
2、变量
// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
var ErrTooLarge = errors.New("bytes.Buffer: too large")
bytes.ErrTooLarge 是 Go 语言标准库中 bytes 包的一个错误变量,表示在执行某些操作时,输入的数据过大,超出了 bytes 包所能处理的范围。
具体来说,当使用 bytes.Buffer 类型的 Write 方法写入数据时,如果写入的数据长度超过了缓冲区的容量,就会返回 bytes.ErrTooLarge 错误。类似地,当使用 bytes.Reader 类型的 Read 方法读取数据时,如果要读取的数据长度超过了缓冲区的长度,也会返回 bytes.ErrTooLarge 错误。
bytes.ErrTooLarge 的作用是提醒开发者注意输入数据的大小,避免因为数据过大而导致程序崩溃或者出现其他问题。
3、函数
3.1 func Compare
函数定义:
func Compare(a, b []byte) int
Compare函数返回一个整数表示两个[]byte切片
按字典序
比较的结果。如果a==b返回0;如果a<b返回-1;否则返回+1。nil参数视为空切片。
func main() {
fmt.Println(bytes.Compare([]byte{},[]byte{})) // 0
fmt.Println(bytes.Compare([]byte{1},[]byte{2})) // -1
fmt.Println(bytes.Compare([]byte{2},[]byte{1})) // 1
fmt.Println(bytes.Compare([]byte{},nil)) //0
fmt.Println([]byte{} == nil) // false
}
3.2 func Equal
函数定义:
func Equal(a, b []byte) bool
判断两个切片的内容是否完全相同。
注意:nil 参数视为空切片。
func main() {
fmt.Println(bytes.Equal([]byte{},[]byte{})) // true
fmt.Println(bytes.Equal([]byte{'A', 'B'},[]byte{'a'})) // false
fmt.Println(bytes.Equal([]byte{'a'},[]byte{'a'})) // true
fmt.Println(bytes.Equal([]byte{},nil)) // true
fmt.Println([]byte{} == nil) // false
}
3.3 func EqualFold
函数定义:
func EqualFold(s, t []byte) bool
判断两个UTF-8编码的字符串s和t是否在Unicode的折叠大小写规则下相等,这种规则比简单的大小写不敏感更加通用。也就是说,EqualFold函数会将两个字符串中的字符先转换为相同的大小写形式,再进行比较,从而判断它们是否相等。
func main() {
fmt.Println(bytes.EqualFold([]byte{},[]byte{})) // true
fmt.Println(bytes.EqualFold([]byte{'A'},[]byte{'a'})) // true
fmt.Println(bytes.EqualFold([]byte{'B'},[]byte{'a'})) // false
fmt.Println(bytes.EqualFold([]byte{},nil)) // true
}
3.4 func Runes
函数定义:
func Runes(s []byte) []rune
Runes函数返回和s等价的[]rune切片。(将utf-8编码的unicode码值分别写入单个rune)
func main() {
fmt.Println([]byte("你好world")) // [228 189 160 229 165 189 119 111 114 108 100]
fmt.Println(bytes.Runes([]byte("你好world"))) // [20320 22909 119 111 114 108 100]
fmt.Println([]rune("你好world")) // [20320 22909 119 111 114 108 100]
}
3.5 func HasPrefix
函数定义:
func HasPrefix(s, prefix []byte) bool
判断s是否有前缀切片prefix。
3.6 func HasSuffix
函数定义:
func HasSuffix(s, suffix []byte) bool
判断s是否有后缀切片suffix。
3.7 func Contains
函数定义:
func Contains(b, subslice []byte) bool
判断切片b是否包含子切片subslice。
3.8 func Count
函数定义:
func Count(s, sep []byte) int
Count计算s中有多少个不重叠的sep子切片。
3.9 func Index
函数定义:
func Index(s, sep []byte) int
子切片sep在s中第一次出现的位置,不存在则返回-1。
3.10 func IndexByte
函数定义:
func IndexByte(s []byte, c byte) int
字符c在s中第一次出现的位置,不存在则返回-1。
3.11 func ndexRune
函数定义:
func IndexRune(s []byte, r rune) int
unicode字符r的utf-8编码在s中第一次出现的位置,不存在则返回-1。
3.12 func IndexAny
函数定义:
func IndexAny(s []byte, chars string) int
字符串chars中的任一utf-8编码在s中第一次出现的位置,如不存在或者chars为空字符串则返回-1
packa