shad-go/lectures/09-io/teereader/main.go

29 lines
371 B
Go
Raw Normal View History

2020-04-23 01:40:13 +00:00
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"strings"
)
func main() {
r := strings.NewReader("some io.Reader stream to be read\n")
var buf bytes.Buffer
tee := io.TeeReader(r, &buf)
printall := func(r io.Reader) {
b, err := ioutil.ReadAll(r)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", b)
}
printall(tee)
printall(&buf)
}