bytes包提供了操作byte 的方法

类型转换

可以将string通过函数byte[]转化为字符切片,同时也可通过string函数将切片转换为string

常用函数: 见strings包

常用结构体

  • Buffer

go语言提供了可变大小的字节缓冲区Buffer,实现了io.Reader 和 io.Writer 接口,可以当作流来写入和读取
常用函数

  • NewBuffer: 使用字符切片创建buffer对象
  • NewBufferString: 使用字符串创建buffer对象

常用方法

  • Bytes: 将buffer对象转换为字节切片
  • String:将buffer对象转换为字符串
  • Read: 读取字符串并放入到切片中
  • ReadFrom: 从流中读入
  • ReadString: 读取字符串,碰到分隔符位置
  • ReadBytes:读取字符切片,碰到分隔符位置
  • Write: 将字节切片写入到buffer对象
  • WriteByte:将字符切片写入到buffer对象
  • WriteRune:将rune写入到buffer对象
  • WriteString: 将字符串写入到buffer对象
  • WriteTo: 将buffer对象中字符串写入到输出流中
  • Reset: 清空内容
    • Reader

go语言提供Reader结构体,可以将字符切片转换为Reader对象,当作流来读取
常用函数

  • NewReader:根据字符串创建reader对象指针

常用方法

  • Read: 读取字节切片并写入到参数切片中
  • WriteTo: 将reader对象中字符串写入到输出流中
  • Reset:更新reader对象字节切片内容
  • Seek: 设置reader对象处理位置

    实操

    learnBytes.go

    ```go package learnBytes

import ( “bytes” “fmt” “os” )

func LearnBytes() { // 类型转换 fmt.Println([]byte(“hello,my name is ligz”)) // 字符串转[]byte fmt.Println(string([]byte(“mynameisligz”))) // 在转成字符串

  1. // 常用函数
  2. fmt.Println(bytes.Compare([]byte("ligzcompare"), []byte("ligzaaabbb")))
  3. fmt.Println(bytes.Contains([]byte("abcdefabc"), []byte("cde")))
  4. fmt.Println(bytes.Count([]byte("abcdefabc"), []byte("cde")))
  5. fmt.Printf("%#v\n", bytes.Fields([]byte("ab c\nde\tfa\fbb\r\nc")))
  6. fmt.Println(bytes.HasPrefix([]byte("abcdefabc"), []byte("abc")))
  7. fmt.Println(bytes.HasSuffix([]byte("abcdefabc"), []byte("abc")))
  8. fmt.Println(bytes.Index([]byte("abcdefabc"), []byte("abc")))
  9. fmt.Println(bytes.LastIndex([]byte("abcdefabc"), []byte("abc")))
  10. fmt.Printf("%#v\n", bytes.Split([]byte("abcdefabc"), []byte("b")))
  11. fmt.Printf("%#v\n", bytes.SplitN([]byte("abcdefabc"), []byte("b"), 2))
  12. fmt.Println(bytes.Join([][]byte{[]byte("abc"), []byte("bcd"), []byte("cde")}, []byte("-")))
  13. fmt.Println(bytes.Map(func(r rune) rune {
  14. return r - 32
  15. }, []byte("abc")))
  16. fmt.Println(bytes.Replace([]byte("abcabcabcdefabc"), []byte("abc"), []byte("x"), 2))
  17. fmt.Println(bytes.ReplaceAll([]byte("abcabcabcdefabc"), []byte("abc"), []byte("x")))
  18. fmt.Println(bytes.Title([]byte("abc defac c")))
  19. fmt.Println(bytes.ToLower([]byte("abc defc c")))
  20. fmt.Println(bytes.ToUpper([]byte("abc defc c")))
  21. fmt.Println(bytes.TrimLeft([]byte("abc defc c"), "a c"))
  22. fmt.Println(bytes.TrimSpace([]byte(" \t \f abc \r\n ")))
  23. fmt.Println(bytes.TrimLeft([]byte("abc defc c"), "a c"))
  24. fmt.Println(bytes.TrimRight([]byte("abc defc c"), "a c "))
  25. fmt.Println(bytes.TrimPrefix([]byte("abc defc c"), []byte("a c")))
  26. fmt.Println(bytes.TrimSuffix([]byte("abc defac c"), []byte("c")))
  27. // 常用结构体
  28. builder := bytes.NewBufferString("abcdefaaa")
  29. fmt.Println(builder.Bytes())
  30. fmt.Println(builder.String())
  31. cxt := make([]byte, 3)
  32. builder.Read(cxt)
  33. fmt.Println(cxt)
  34. fmt.Println(builder.ReadString('\n'))
  35. builder.WriteString("xyz")
  36. fmt.Println(builder.ReadString('\n'))
  37. builder.WriteString("xyz")
  38. builder.Reset()
  39. fmt.Println(builder.ReadString('\n'))
  40. builder.WriteString("xyz")
  41. builder.WriteTo(os.Stdout)
  42. // Reader
  43. var reader *bytes.Reader = bytes.NewReader([]byte("abcdefa"))
  44. var c1 []byte = make([]byte, 10)
  45. fmt.Println(reader.Read(c1))
  46. fmt.Println(string(c1))
  47. var c2 []byte = make([]byte, 10)
  48. reader.Reset([]byte("xyz"))
  49. fmt.Println(reader.Read(c2))
  50. var c3 = make([]byte, 10)
  51. reader.Seek(1, 0)
  52. fmt.Println(reader.Read(c3))
  53. fmt.Println(string(c3))
  54. var c4 = make([]byte, 10)
  55. reader.Seek(10, 0)
  56. fmt.Println(reader.Read(c4))
  57. reader.Seek(0,0)
  58. reader.WriteTo(os.Stdout)

}

  1. <a name="rXhVn"></a>
  2. ### learnBytes_test.go
  3. ```go
  4. package learnBytes
  5. import "testing"
  6. func TestLearnBytes(t *testing.T) {
  7. LearnBytes()
  8. }

执行结果

  1. GOROOT=C:\Program Files\Go #gosetup
  2. GOPATH=C:\Users\ligz\go #gosetup
  3. "C:\Program Files\Go\bin\go.exe" test -c -o C:\Users\ligz\AppData\Local\Temp\GoLand\___basicproject_learnBytes__TestLearnBytes.test.exe basicproject/learnBytes #gosetup
  4. "C:\Program Files\Go\bin\go.exe" tool test2json -t C:\Users\ligz\AppData\Local\Temp\GoLand\___basicproject_learnBytes__TestLearnBytes.test.exe -test.v -test.paniconexit0 -test.run ^\QTestLearnBytes\E$ #gosetup
  5. === RUN TestLearnBytes
  6. [104 101 108 108 111 44 109 121 32 110 97 109 101 32 105 115 32 108 105 103 122]
  7. mynameisligz
  8. 1
  9. true
  10. 1
  11. [][]uint8{[]uint8{0x61, 0x62}, []uint8{0x63}, []uint8{0x64, 0x65}, []uint8{0x66, 0x61}, []uint8{0x62, 0x62}, []uint8{0x63}}
  12. true
  13. true
  14. 0
  15. 6
  16. [][]uint8{[]uint8{0x61}, []uint8{0x63, 0x64, 0x65, 0x66, 0x61}, []uint8{0x63}}
  17. [][]uint8{[]uint8{0x61}, []uint8{0x63, 0x64, 0x65, 0x66, 0x61, 0x62, 0x63}}
  18. [97 98 99 45 98 99 100 45 99 100 101]
  19. [65 66 67]
  20. [120 120 97 98 99 100 101 102 97 98 99]
  21. [120 120 120 100 101 102 120]
  22. [65 98 99 32 68 101 102 97 99 32 67]
  23. [97 98 99 32 100 101 102 99 32 99]
  24. [65 66 67 32 68 69 70 67 32 67]
  25. [98 99 32 100 101 102 99 32 99]
  26. [97 98 99]
  27. [98 99 32 100 101 102 99 32 99]
  28. [97 98 99 32 100 101 102]
  29. [97 98 99 32 100 101 102 99 32 99]
  30. [97 98 99 32 100 101 102 97 99 32]
  31. [97 98 99 100 101 102 97 97 97]
  32. abcdefaaa
  33. [97 98 99]
  34. defaaa EOF
  35. xyz EOF
  36. EOF
  37. xyz7 <nil>
  38. abcdefa
  39. 3 <nil>
  40. 2 <nil>
  41. yz
  42. 0 EOF
  43. xyz--- PASS: TestLearnBytes (0.00s)
  44. PASS
  45. 进程 已完成,退出代码为 0