Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/miolini/datacounter
Golang counters for readers/writers
https://github.com/miolini/datacounter
Last synced: 2 days ago
JSON representation
Golang counters for readers/writers
- Host: GitHub
- URL: https://github.com/miolini/datacounter
- Owner: miolini
- License: mit
- Created: 2015-10-14T19:15:50.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2023-04-13T17:55:42.000Z (almost 2 years ago)
- Last Synced: 2024-07-31T20:52:23.743Z (6 months ago)
- Language: Go
- Size: 20.5 KB
- Stars: 48
- Watchers: 3
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - datacounter - Go counters for readers/writer/http.ResponseWriter. (Miscellaneous / Uncategorized)
- zero-alloc-awesome-go - datacounter - Go counters for readers/writer/http.ResponseWriter. (Miscellaneous / Uncategorized)
- awesome-go - datacounter - Golang counters for readers/writers - ★ 25 (Miscellaneous)
- awesome-go-extra - datacounter - 10-14T19:15:50Z|2022-07-03T21:09:36Z| (Microsoft Office / Uncategorized)
- awesome-go-zh - datacounter
README
# 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))
}
```