• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D06-Feb-2020269

.travis.ymlH A D06-Feb-2020251

LICENSEH A D06-Feb-20201.1 KiB

README.mdH A D06-Feb-20201.2 KiB

go.modH A D06-Feb-202047

reader.goH A D06-Feb-2020596

reader_test.goH A D06-Feb-2020410

response_writer.goH A D06-Feb-20201.7 KiB

response_writer_test.goH A D06-Feb-2020518

writer.goH A D06-Feb-2020594

writer_test.goH A D06-Feb-2020293

README.md

1# Datacounter
2
3Golang counters for readers/writers.
4
5[![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)
6
7## Examples
8
9### ReaderCounter
10
11```go
12buf := bytes.Buffer{}
13buf.Write(data)
14counter := datacounter.NewReaderCounter(&buf)
15
16io.Copy(ioutil.Discard, counter)
17if counter.Count() != dataLen {
18	t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data))
19}
20```
21
22### WriterCounter
23
24```go
25buf := bytes.Buffer{}
26counter := datacounter.NewWriterCounter(&buf)
27
28counter.Write(data)
29if counter.Count() != dataLen {
30	t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data))
31}
32```
33
34### http.ResponseWriter Counter
35
36```go
37handler := func(w http.ResponseWriter, r *http.Request) {
38	w.Write(data)
39}
40
41req, err := http.NewRequest("GET", "http://example.com/foo", nil)
42if err != nil {
43	t.Fatal(err)
44}
45
46w := httptest.NewRecorder()
47counter := datacounter.NewResponseWriterCounter(w)
48
49handler(counter, req)
50if counter.Count() != dataLen {
51	t.Fatalf("count mismatch len of test data: %d != %d", counter.Count(), len(data))
52}
53```
54