github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/talks/2014/go4java/writecounter.go (about) 1 // +build OMIT 2 3 package main 4 5 import ( 6 "bytes" 7 "fmt" 8 "io" 9 "os" 10 ) 11 12 var ( 13 _ = bytes.Buffer{} 14 _ = os.Stdout 15 ) 16 17 // WriteCounter tracks the total number of bytes written. 18 type WriteCounter struct { 19 io.ReadWriter 20 count int 21 } 22 23 func (w *WriteCounter) Write(b []byte) (int, error) { 24 w.count += len(b) 25 return w.ReadWriter.Write(b) 26 } 27 28 // MAIN OMIT 29 func main() { 30 buf := &bytes.Buffer{} 31 w := &WriteCounter{ReadWriter: buf} 32 33 fmt.Fprintf(w, "Hello, gophers!\n") 34 fmt.Printf("Printed %v bytes", w.count) 35 }