Example: How to use TeeReader from IO Package in Golang?
TeeReader returns a Reader that writes to w what it reads from r. All reads from r performed through it are matched with corresponding writes to w.
Example
package main
import (
"bytes"
"io"
"fmt"
"strings"
)
func main() {
testString := strings.NewReader("Jobs, Code, Videos and News for Go hackers.")
var bufferRead bytes.Buffer
example := io.TeeReader(testString, &bufferRead)
readerMap := make([]byte, testString.Len())
length, err := example.Read(readerMap)
fmt.Printf("\nBufferRead: %s", &bufferRead)
fmt.Printf("\nRead: %s", readerMap)
fmt.Printf("\nLength: %d, Error:%v", length, err)
}
Output
BufferRead: Jobs, Code, Videos and News for Go hackers.
Read: Jobs, Code, Videos and News for Go hackers.
Length: 43, Error:
Most Helpful This Week
Golang Concurrency Best Practices
Copy Struct Type Using Value and Pointer Reference
Golang write CSV records
Convert Int data type to Int16 Int32 Int64
How do you handle HTTP server HTTP/2 in Go?
Naming Conventions for Golang Functions
GO Program to Swap Number Without Using Temporary Variables
How to remove all line breaks from a string in Golang?
Example: ReadAll, ReadDir, and ReadFile from IO Package
Cannot use <variable> as <type> value in return statement error in Golang