io.TeeReader() Function in Golang with Examples

In Go language, io packages supply fundamental interfaces to the I/O primitives. And its principal job is to enclose the ongoing implementations of such king of primitives. The TeeReader() function in Go language is used to return a “Reader” that reads from the stated “r” and then writes it to the stated “w”. And here all the reads that are executed through the stated “r” are compared with the equivalent writes to the stated “w”. This method doesn’t require any internal buffering and the write is completed once the read completes. Moreover, this function is defined under the io package. Here, you need to import the “io” package in order to use these functions.

Syntax:

func TeeReader(r Reader, w Writer) Reader

Here, “r” is the stated Reader and “w” is the stated Writer.

Return value: It returns a “Reader” that reads from the stated “r” and then writes it to the given “w”. However, any error faced while writing the content is returned as a read error.

Below examples illustrates the use of above method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.TeeReader() function
  
// Including main package
package main
  
// Importing fmt, io, strings, bytes, and os
import (
    "bytes"
    "fmt"
    "io"
    "os"
    "strings"
)
  
// Calling main
func main() {
  
    // Defining reader using NewReader method
    reader := strings.NewReader("GfG\n")
  
    // Defining buffer
    var buf bytes.Buffer
  
    // Calling TeeReader method with its parameters
    r := io.TeeReader(reader, &buf)
  
    // Calling Copy method with its parameters
    Reader, err := io.Copy(os.Stdout, r)
  
    // If error is not nil then panics
    if err != nil {
        panic(err)
    }
  
    // Prints output
    fmt.Printf("n: %v\n", Reader)
}

chevron_right


Output:

GfG
n: 4

In the above example, Copy() method is used in order to return the “Reader”, NewReader() method of strings is used from where the content to be read is written and an external buffer is also used here.

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate the usage of
// io.TeeReader() function
  
// Including main package
package main
  
// Importing fmt, io, strings, bytes, and os
import (
    "bytes"
    "fmt"
    "io"
    "os"
    "strings"
)
  
// Calling main
func main() {
  
    // Defining reader using NewReader method
    reader := strings.NewReader("GeeksforGeeks\nis\na\nCS-Portal.\n")
  
    // Defining buffer
    var buf bytes.Buffer
  
    // Calling TeeReader method with its parameters
    r := io.TeeReader(reader, &buf)
  
    // Calling Copy method with its parameters
    Reader, err := io.Copy(os.Stdout, r)
  
    // If error is not nil then panics
    if err != nil {
        panic(err)
    }
  
    // Prints output
    fmt.Printf("n: %v\n", Reader)
}

chevron_right


Output:

GeeksforGeeks
is
a
CS-Portal.
n: 30



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.