Created
March 5, 2013 16:23
-
-
Save jgrahamc/5091524 to your computer and use it in GitHub Desktop.
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 // 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