Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rosbit/reader-logger
a utility to output the reader content and process it simultaneously.
https://github.com/rosbit/reader-logger
Last synced: about 6 hours ago
JSON representation
a utility to output the reader content and process it simultaneously.
- Host: GitHub
- URL: https://github.com/rosbit/reader-logger
- Owner: rosbit
- License: mit
- Created: 2022-12-16T00:21:04.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-29T02:43:15.000Z (about 1 year ago)
- Last Synced: 2023-10-29T03:23:18.957Z (about 1 year ago)
- Language: Go
- Size: 2.93 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# reader-logger, a utility to output the reader content and process it simultaneously.
## usage
```go
import (
logr "github.com/rosbit/reader-logger"
"encoding/json"
"bytes"
"os"
"io"
"fmt"
)func main() {
// a reader for testing
reader := bytes.NewBufferString(`{"name":"rosbit", "age": 10}`)// j to store the result
var j struct {
Name string `json:"name"`
Age int `json:"age"`
}if err := parseJSON(reader, &j); err != nil {
fmt.Printf("err: %v\n", err)
return
}
fmt.Printf("result: %v\n", j)
}// print the content in reader and parse it.
func parseJSON(reader io.Reader, j interface{}) error {
r, deferFunc := logr.ReaderLogger(reader, os.Stderr, "data in reader") // create a new reader
defer deferFunc()return json.NewDecoder(r).Decode(j) // parse JSON in the new reader
}
```## result
run `go test`, the result is as following:
```
--- data in reader begin ---
{"name":"rosbit", "age": 10}
--- data in reader end ---
result: {rosbit 10}
PASS
ok github.com/rosbit/reader-logger 0.685s
```you can output the content of reader in any io.Writer other than os.Stderr as in the sample, of course.