-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo80.go
86 lines (71 loc) · 2.64 KB
/
demo80.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bytes"
"fmt"
)
func main() {
// 示例1
contents := "ab"
buffer1 := bytes.NewBufferString(contents)
fmt.Printf("The capacity of new buffer with contents %q: %d\n", contents, buffer1.Cap())
fmt.Println()
unreadBytes := buffer1.Bytes()
fmt.Printf("The unread bytes of the buffer: %v\n", unreadBytes)
fmt.Println()
contents = "cdefg"
fmt.Printf("Write contents %q ...\n", contents)
buffer1.WriteString(contents)
fmt.Printf("The capacity of buffer: %d\n", buffer1.Cap())
fmt.Println()
// 只要扩充一下之前拿到的未读字节切片unreadBytes
// 就可以用它来读取甚至修改buffer中的后续内容
unreadBytes = unreadBytes[:cap(unreadBytes)]
fmt.Printf("The unread bytes of the buffer: %v\n", unreadBytes)
fmt.Println()
value := byte('X')
fmt.Printf("Set a byte in the unread bytes to %v ...\n", value)
unreadBytes[len(unreadBytes)-2] = value
fmt.Printf("The unread bytes of the buffer: %v\n", buffer1.Bytes())
fmt.Println()
// 不过,在buffer的内容容器真正扩容之后就无法这么做
contents = "hijklmn"
fmt.Printf("Write contents %q ...\n", contents)
buffer1.WriteString(contents)
fmt.Printf("The capacity of buffer: %d\n", buffer1.Cap())
fmt.Println()
unreadBytes = unreadBytes[:cap(unreadBytes)]
fmt.Printf("The unread bytes of the buffer: %v\n", unreadBytes)
fmt.Println()
// 示例2
// Next方法返回的后续字节切片也存在相同的问题
contents = "12"
buffer2 := bytes.NewBufferString(contents)
fmt.Printf("The capacity of new buffer with contents %q: %d\n", contents, buffer2.Cap())
fmt.Println()
nextBytes := buffer2.Next(2)
fmt.Printf("The next bytes of the buffer: %v\n", nextBytes)
fmt.Println()
contents = "34567"
fmt.Printf("Write contents %q ...\n", contents)
buffer2.WriteString(contents)
fmt.Printf("The capacity of buffer: %d\n", buffer2.Cap())
fmt.Println()
// 只要扩充一下之前拿到的未读字节切片nextBytes
// 就可以用它来读取甚至修改buffer中的后续内容
nextBytes = nextBytes[:cap(nextBytes)]
fmt.Printf("The next bytes of the buffer: %v\n", nextBytes)
fmt.Println()
value = byte('X')
fmt.Printf("Set a byte in the next bytes to %v ...\n", value)
nextBytes[len(nextBytes)-2] = value
fmt.Printf("The next bytes of the buffer: %v\n", buffer2.Bytes())
fmt.Println()
// 不过,在buffer的内容容器真正扩容之后就无法这么做
contents = "89101112"
fmt.Printf("Write contents %q ...\n", contents)
buffer2.WriteString(contents)
fmt.Printf("The capacity of buffer: %d\n", buffer2.Cap())
fmt.Println()
nextBytes = nextBytes[:cap(nextBytes)]
fmt.Printf("The next bytes of the buffer: %v\n", nextBytes)
}