Skip to content

Instantly share code, notes, and snippets.

@jgrahamc
Created March 5, 2013 16:23
Show Gist options
  • Save jgrahamc/5091524 to your computer and use it in GitHub Desktop.
Save jgrahamc/5091524 to your computer and use it in GitHub Desktop.
package counter
import (
"io"
)
type Counter struct {
w io.Writer // Underlying writer to send data to
c int // Number of bytes written since last call to Count()
}
func New(w io.Writer) (h *Counter) {
h = new(Counter)
h.w = w
return
}
// Write: standard io.Writer interface. To use this package call
// Write continually. This will both count the bytes written and
// write to the underlying writer.
func (h *Counter) Write(p []byte) (n int, err error) {
n, err = h.w.Write(p)
h.c += n
return
}
// Count: returns the total number of bytes that were written to the
// underlying writer since the last call to Count
func (h *Counter) Count() (c int) {
c = h.c
h.c = 0
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment