Standard library / io

io.TeeReader()

Copies bytes actually read into a writer before returning them; writer failures become read errors. No internal buffering. Expected stdout: 'ab ab\n'.

Formal Syntax & Structure

go syntaxgo
func TeeReader(r Reader, w Writer) Reader

Parameters & Attributes

NameTypeDescription
rReaderSource reader.
wWriterDestination writer.

Return Value

Reader

Copies bytes actually read into a writer before returning them; writer failures become read errors.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "io"  "strings")
func main() {  var b bytes.Buffer  r := io.TeeReader(strings.NewReader("abc"), &b)  p := make([]byte, 2)  r.Read(p)  fmt.Println(string(p), b.String())}
Go Reference