-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
46 lines (41 loc) · 1.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Write a function `CountingWriter` with the signature below that, given an
// `io.Writer`, returns a new `Writer` that wraps the original, and a pointer to an
// `int64` variable that any moment contains the number of bytes written to the new
// `Writer`.
// ```
// func CounterWriter(w io.Writer) (io.Writer, *int64)
// ```
package main
import (
"fmt"
"io"
"os"
)
// WriterCounter wraps an `io.Writer` object and provides an `int64` counter
type WriterCounter struct {
Counter int64
Writer io.Writer
}
func main() {
w, c := CounterWriter(os.Stdout)
fmt.Fprint(w, "I know not all\t\t")
fmt.Printf("[%d bytes]\n", *c)
fmt.Fprint(w, "that may be coming,\t")
fmt.Printf("[%d bytes]\n", *c)
fmt.Fprint(w, "but be it what it will,\t")
fmt.Printf("[%d bytes]\n", *c)
fmt.Fprint(w, "I’ll go to it laughing.\t")
fmt.Printf("[%d bytes]\n", *c)
}
// Write counts the number of bytes written as it writes to a `WriterCounter.Writer`
func (c *WriterCounter) Write(p []byte) (int, error) {
c.Counter += int64(len(p))
return c.Writer.Write(p)
}
// CounterWriter wraps an `io.Writer` in a `WriterCounter` which provides counting
func CounterWriter(w io.Writer) (io.Writer, *int64) {
wc := new(WriterCounter)
wc.Counter = 0
wc.Writer = w
return wc, &wc.Counter
}