Skip to content

Commit

Permalink
bytes: add example with (*Buffer).Cap, (*Buffer).Read, (*Buffer).Read…
Browse files Browse the repository at this point in the history
…Byte

Change-Id: Ieb107fdfccde9f054491f667a384b16f7af71dea
Reviewed-on: https://go-review.googlesource.com/c/go/+/355289
Run-TryBot: Ian Lance Taylor <[email protected]>
TryBot-Result: Go Bot <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Trust: Cherry Mui <[email protected]>
  • Loading branch information
180909 authored and ianlancetaylor committed Nov 5, 2021
1 parent 4c7cafd commit 3e9e024
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/bytes/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e9e024

Please sign in to comment.