Created
July 3, 2012 14:01
-
-
Save jgrahamc/3039892 to your computer and use it in GitHub Desktop.
Counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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