aboutsummaryrefslogtreecommitdiff
path: root/src/bytes
diff options
context:
space:
mode:
authorjiahua wang <wjh180909@gmail.com>2021-10-12 14:27:02 +0800
committerIan Lance Taylor <iant@golang.org>2021-11-05 21:26:54 +0000
commit3e9e02412e7770e46c7e725e17dee09a7d79f32c (patch)
tree15b30aba25bdc859498ee5ec8585fa391c1397c5 /src/bytes
parent4c7cafdd03426bc2b9fb1275d13d0abc755dde16 (diff)
downloadgo-3e9e02412e7770e46c7e725e17dee09a7d79f32c.tar.gz
go-3e9e02412e7770e46c7e725e17dee09a7d79f32c.zip
bytes: add example with (*Buffer).Cap, (*Buffer).Read, (*Buffer).ReadByte
Change-Id: Ieb107fdfccde9f054491f667a384b16f7af71dea Reviewed-on: https://go-review.googlesource.com/c/go/+/355289 Run-TryBot: Ian Lance Taylor <iant@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org> Trust: Cherry Mui <cherryyz@google.com>
Diffstat (limited to 'src/bytes')
-rw-r--r--src/bytes/example_test.go43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/bytes/example_test.go b/src/bytes/example_test.go
index d04b088fab..54a7aa6ae6 100644
--- a/src/bytes/example_test.go
+++ b/src/bytes/example_test.go
@@ -37,6 +37,16 @@ func ExampleBuffer_Bytes() {
// Output: hello world
}
+func ExampleBuffer_Cap() {
+ buf1 := bytes.NewBuffer(make([]byte, 10))
+ buf2 := bytes.NewBuffer(make([]byte, 0, 10))
+ fmt.Println(buf1.Cap())
+ fmt.Println(buf2.Cap())
+ // Output:
+ // 10
+ // 10
+}
+
func ExampleBuffer_Grow() {
var b bytes.Buffer
b.Grow(64)
@@ -67,6 +77,39 @@ func ExampleBuffer_Next() {
// e
}
+func ExampleBuffer_Read() {
+ var b bytes.Buffer
+ b.Grow(64)
+ b.Write([]byte("abcde"))
+ rdbuf := make([]byte, 1)
+ n, err := b.Read(rdbuf)
+ if err != nil {
+ panic(err)
+ }
+ fmt.Println(n)
+ fmt.Println(b.String())
+ fmt.Println(string(rdbuf))
+ // Output
+ // 1
+ // bcde
+ // a
+}
+
+func ExampleBuffer_ReadByte() {
+ var b bytes.Buffer
+ b.Grow(64)
+ b.Write([]byte("abcde"))
+ c, err := b.ReadByte()
+ if err != nil {
+ panic(err)
+ }
+ fmt.Println(c)
+ fmt.Println(b.String())
+ // Output
+ // 97
+ // bcde
+}
+
func ExampleCompare() {
// Interpret Compare's result by comparing it to zero.
var a, b []byte