mirror of
https://github.com/golang/go
synced 2024-11-15 03:40:29 -07:00
bufio: add example for ReadFrom and remove unused code
Change-Id: Ia4fbb436ca573b1820f2b4d06d2332f588334768 Reviewed-on: https://go-review.googlesource.com/c/go/+/624357 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Ian Lance Taylor <iant@google.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
parent
65235e137f
commit
2fd2718f6b
@ -939,7 +939,6 @@ func (t *testReader) Read(buf []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testReadLine(t *testing.T, input []byte) {
|
func testReadLine(t *testing.T, input []byte) {
|
||||||
//for stride := 1; stride < len(input); stride++ {
|
|
||||||
for stride := 1; stride < 2; stride++ {
|
for stride := 1; stride < 2; stride++ {
|
||||||
done := 0
|
done := 0
|
||||||
reader := testReader{input, stride}
|
reader := testReader{input, stride}
|
||||||
|
@ -33,6 +33,33 @@ func ExampleWriter_AvailableBuffer() {
|
|||||||
// Output: 1 2 3 4
|
// Output: 1 2 3 4
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ExampleWriter_ReadFrom demonstrates how to use the ReadFrom method of Writer.
|
||||||
|
func ExampleWriter_ReadFrom() {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
writer := bufio.NewWriter(&buf)
|
||||||
|
|
||||||
|
data := "Hello, world!\nThis is a ReadFrom example."
|
||||||
|
reader := strings.NewReader(data)
|
||||||
|
|
||||||
|
n, err := writer.ReadFrom(reader)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("ReadFrom Error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = writer.Flush(); err != nil {
|
||||||
|
fmt.Println("Flush Error:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println("Bytes written:", n)
|
||||||
|
fmt.Println("Buffer contents:", buf.String())
|
||||||
|
// Output:
|
||||||
|
// Bytes written: 41
|
||||||
|
// Buffer contents: Hello, world!
|
||||||
|
// This is a ReadFrom example.
|
||||||
|
}
|
||||||
|
|
||||||
// The simplest use of a Scanner, to read standard input as a set of lines.
|
// The simplest use of a Scanner, to read standard input as a set of lines.
|
||||||
func ExampleScanner_lines() {
|
func ExampleScanner_lines() {
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
Loading…
Reference in New Issue
Block a user