]> Cypherpunks repositories - gostls13.git/commitdiff
bufio: add example for ReadFrom and remove unused code
authorcuishuang <imcusg@gmail.com>
Fri, 1 Nov 2024 09:55:50 +0000 (17:55 +0800)
committerGopher Robot <gobot@golang.org>
Fri, 1 Nov 2024 21:52:12 +0000 (21:52 +0000)
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>
src/bufio/bufio_test.go
src/bufio/example_test.go

index 60752d38f65c31c84d141947a3cb2004d998daee..63dd2ea4322cea1f3c4bd2a7aa12797c8a95902d 100644 (file)
@@ -939,7 +939,6 @@ func (t *testReader) Read(buf []byte) (n int, err error) {
 }
 
 func testReadLine(t *testing.T, input []byte) {
-       //for stride := 1; stride < len(input); stride++ {
        for stride := 1; stride < 2; stride++ {
                done := 0
                reader := testReader{input, stride}
index 6d219aecc6584a703f600b84c3566e802dbc3b3d..7d4f0c1c28dce2d27f7d74b974e5f4746818d716 100644 (file)
@@ -33,6 +33,33 @@ func ExampleWriter_AvailableBuffer() {
        // 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.
 func ExampleScanner_lines() {
        scanner := bufio.NewScanner(os.Stdin)