Skip to content

Instantly share code, notes, and snippets.

@jgrahamc
Created July 3, 2012 14:01
Show Gist options
  • Save jgrahamc/3039892 to your computer and use it in GitHub Desktop.
Save jgrahamc/3039892 to your computer and use it in GitHub Desktop.
Counter
package counter
import "io"
type Counter struct {
w io.Writer
c int
}
func New(w io.Writer) (h *Counter) {
h = new(Counter)
h.w = w
h.c = 0
return
}
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 Clear
func (h *Counter) Count() (int) {
return h.c
}
// Clear: zeroes the write byte counter
func (h *Counter) Clear() {
h.c = 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment