File: README.md

package info (click to toggle)
golang-github-miolini-datacounter 1.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 100 kB
  • sloc: makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,234 bytes parent folder | download
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
47
48
49
50
51
52
53
# Datacounter

Golang counters for readers/writers.

[![Build Status](https://travis-ci.org/miolini/datacounter.svg)](https://travis-ci.org/miolini/datacounter) [![GoDoc](https://godoc.org/github.com/miolini/datacounter?status.svg)](http://godoc.org/github.com/miolini/datacounter)

## Examples

### ReaderCounter

```go
buf := bytes.Buffer{}
buf.Write(data)
counter := datacounter.NewReaderCounter(&buf)

io.Copy(ioutil.Discard, counter)
if counter.Count() != dataLen {
	t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data))
}
```

### WriterCounter

```go
buf := bytes.Buffer{}
counter := datacounter.NewWriterCounter(&buf)

counter.Write(data)
if counter.Count() != dataLen {
	t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data))
}
```

### http.ResponseWriter Counter

```go
handler := func(w http.ResponseWriter, r *http.Request) {
	w.Write(data)
}

req, err := http.NewRequest("GET", "http://example.com/foo", nil)
if err != nil {
	t.Fatal(err)
}

w := httptest.NewRecorder()
counter := datacounter.NewResponseWriterCounter(w)

handler(counter, req)
if counter.Count() != dataLen {
	t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data))
}
```