github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2014/go4gophers/chain.go (about)

     1  // +build OMIT
     2  
     3  package main
     4  
     5  import (
     6  	"io"
     7  	"io/ioutil"
     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  func main() {
    32  	// START OMIT
    33  	var r io.Reader = ByteReader('A')
    34  	r = io.LimitReader(r, 1e6)
    35  	r = LogReader{r}
    36  	io.Copy(ioutil.Discard, r)
    37  	// STOP OMIT
    38  
    39  	return
    40  	io.Copy(ioutil.Discard, LogReader{io.LimitReader(ByteReader('A'), 1e6)})
    41  }