github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2014/go4gophers/reader.go (about) 1 // +build OMIT 2 3 package main 4 5 import ( 6 "fmt" 7 "io" 8 "log" 9 ) 10 11 // ByteReader implements an io.Reader that emits a stream of its byte value. 12 type ByteReader byte 13 14 func (b ByteReader) Read(buf []byte) (int, error) { 15 for i := range buf { 16 buf[i] = byte(b) 17 } 18 return len(buf), nil 19 } 20 21 type LogReader struct { 22 io.Reader 23 } 24 25 func (r LogReader) Read(b []byte) (int, error) { 26 n, err := r.Reader.Read(b) 27 log.Printf("read %d bytes, error: %v", n, err) 28 return n, err 29 } 30 31 // STOP OMIT 32 33 func main() { 34 // START OMIT 35 r := LogReader{ByteReader('A')} 36 b := make([]byte, 10) 37 r.Read(b) 38 fmt.Printf("b: %q", b) 39 // STOP OMIT 40 }