类型转换
可以将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”))) // 在转成字符串
// 常用函数
fmt.Println(bytes.Compare([]byte("ligzcompare"), []byte("ligzaaabbb")))
fmt.Println(bytes.Contains([]byte("abcdefabc"), []byte("cde")))
fmt.Println(bytes.Count([]byte("abcdefabc"), []byte("cde")))
fmt.Printf("%#v\n", bytes.Fields([]byte("ab c\nde\tfa\fbb\r\nc")))
fmt.Println(bytes.HasPrefix([]byte("abcdefabc"), []byte("abc")))
fmt.Println(bytes.HasSuffix([]byte("abcdefabc"), []byte("abc")))
fmt.Println(bytes.Index([]byte("abcdefabc"), []byte("abc")))
fmt.Println(bytes.LastIndex([]byte("abcdefabc"), []byte("abc")))
fmt.Printf("%#v\n", bytes.Split([]byte("abcdefabc"), []byte("b")))
fmt.Printf("%#v\n", bytes.SplitN([]byte("abcdefabc"), []byte("b"), 2))
fmt.Println(bytes.Join([][]byte{[]byte("abc"), []byte("bcd"), []byte("cde")}, []byte("-")))
fmt.Println(bytes.Map(func(r rune) rune {
return r - 32
}, []byte("abc")))
fmt.Println(bytes.Replace([]byte("abcabcabcdefabc"), []byte("abc"), []byte("x"), 2))
fmt.Println(bytes.ReplaceAll([]byte("abcabcabcdefabc"), []byte("abc"), []byte("x")))
fmt.Println(bytes.Title([]byte("abc defac c")))
fmt.Println(bytes.ToLower([]byte("abc defc c")))
fmt.Println(bytes.ToUpper([]byte("abc defc c")))
fmt.Println(bytes.TrimLeft([]byte("abc defc c"), "a c"))
fmt.Println(bytes.TrimSpace([]byte(" \t \f abc \r\n ")))
fmt.Println(bytes.TrimLeft([]byte("abc defc c"), "a c"))
fmt.Println(bytes.TrimRight([]byte("abc defc c"), "a c "))
fmt.Println(bytes.TrimPrefix([]byte("abc defc c"), []byte("a c")))
fmt.Println(bytes.TrimSuffix([]byte("abc defac c"), []byte("c")))
// 常用结构体
builder := bytes.NewBufferString("abcdefaaa")
fmt.Println(builder.Bytes())
fmt.Println(builder.String())
cxt := make([]byte, 3)
builder.Read(cxt)
fmt.Println(cxt)
fmt.Println(builder.ReadString('\n'))
builder.WriteString("xyz")
fmt.Println(builder.ReadString('\n'))
builder.WriteString("xyz")
builder.Reset()
fmt.Println(builder.ReadString('\n'))
builder.WriteString("xyz")
builder.WriteTo(os.Stdout)
// Reader
var reader *bytes.Reader = bytes.NewReader([]byte("abcdefa"))
var c1 []byte = make([]byte, 10)
fmt.Println(reader.Read(c1))
fmt.Println(string(c1))
var c2 []byte = make([]byte, 10)
reader.Reset([]byte("xyz"))
fmt.Println(reader.Read(c2))
var c3 = make([]byte, 10)
reader.Seek(1, 0)
fmt.Println(reader.Read(c3))
fmt.Println(string(c3))
var c4 = make([]byte, 10)
reader.Seek(10, 0)
fmt.Println(reader.Read(c4))
reader.Seek(0,0)
reader.WriteTo(os.Stdout)
}
<a name="rXhVn"></a>
### learnBytes_test.go
```go
package learnBytes
import "testing"
func TestLearnBytes(t *testing.T) {
LearnBytes()
}
执行结果
GOROOT=C:\Program Files\Go #gosetup
GOPATH=C:\Users\ligz\go #gosetup
"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
"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
=== RUN TestLearnBytes
[104 101 108 108 111 44 109 121 32 110 97 109 101 32 105 115 32 108 105 103 122]
mynameisligz
1
true
1
[][]uint8{[]uint8{0x61, 0x62}, []uint8{0x63}, []uint8{0x64, 0x65}, []uint8{0x66, 0x61}, []uint8{0x62, 0x62}, []uint8{0x63}}
true
true
0
6
[][]uint8{[]uint8{0x61}, []uint8{0x63, 0x64, 0x65, 0x66, 0x61}, []uint8{0x63}}
[][]uint8{[]uint8{0x61}, []uint8{0x63, 0x64, 0x65, 0x66, 0x61, 0x62, 0x63}}
[97 98 99 45 98 99 100 45 99 100 101]
[65 66 67]
[120 120 97 98 99 100 101 102 97 98 99]
[120 120 120 100 101 102 120]
[65 98 99 32 68 101 102 97 99 32 67]
[97 98 99 32 100 101 102 99 32 99]
[65 66 67 32 68 69 70 67 32 67]
[98 99 32 100 101 102 99 32 99]
[97 98 99]
[98 99 32 100 101 102 99 32 99]
[97 98 99 32 100 101 102]
[97 98 99 32 100 101 102 99 32 99]
[97 98 99 32 100 101 102 97 99 32]
[97 98 99 100 101 102 97 97 97]
abcdefaaa
[97 98 99]
defaaa EOF
xyz EOF
EOF
xyz7 <nil>
abcdefa
3 <nil>
2 <nil>
yz
0 EOF
xyz--- PASS: TestLearnBytes (0.00s)
PASS
进程 已完成,退出代码为 0