https://github.com/billsjc/io-helper
a tool to reuse io.Reader
https://github.com/billsjc/io-helper
Last synced: about 1 month ago
JSON representation
a tool to reuse io.Reader
- Host: GitHub
- URL: https://github.com/billsjc/io-helper
- Owner: BillSJC
- License: mit
- Created: 2019-05-21T02:10:12.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-05-21T03:33:11.000Z (about 7 years ago)
- Last Synced: 2025-03-02T23:43:50.076Z (over 1 year ago)
- Language: Go
- Size: 9.77 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# io-helper
An IO util to help use to reuse `io.Reader`
[](https://goreportcard.com/report/github.com/BillSJC/io-helper)
[](https://codecov.io/gh/BillSJC/io-helper)
[](https://travis-ci.com/BillSJC/io-helper)
[](https://www.codetriage.com/billsjc/io-helper)
## Install
```bash
go get -u github.com/BillSJC/io-helper
```
## Sample
By using func `io_helper.GetRequestPayload(r)`, u can read `Request.Body` without remove it
```go
package main
import (
"fmt"
io_helper "io-helper"
"net/http"
)
func IndexHandler(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover() ; err != nil {
fmt.Println("recover")
fmt.Println(err)
}
}()
//first time get Data
data,err := io_helper.GetRequestPayload(r)
if err != nil {
panic(err)
}
//print
fmt.Printf("First time data : %s \n",data)
//second time get Data
data2,err := io_helper.GetRequestPayload(r)
if err != nil {
panic(err)
}
//print
fmt.Printf("First time data : %s \n",data2)
//call foo()
foo(r)
}
func foo(r *http.Request){
//second time get Data
data3,err := io_helper.GetRequestPayload(r)
if err != nil {
panic(err)
}
//print
fmt.Printf("Third time data : %s \n",data3)
}
func main() {
http.HandleFunc("/", IndexHandler)
http.ListenAndServe("127.0.0.0:8888", nil)
}
```