https://github.com/stevenferrer/gread
A library for reading and parsing inputs.
https://github.com/stevenferrer/gread
go golang gread input reader scanner
Last synced: 6 months ago
JSON representation
A library for reading and parsing inputs.
- Host: GitHub
- URL: https://github.com/stevenferrer/gread
- Owner: stevenferrer
- License: mit
- Created: 2017-11-26T09:24:14.000Z (over 8 years ago)
- Default Branch: main
- Last Pushed: 2021-11-04T00:59:54.000Z (over 4 years ago)
- Last Synced: 2025-04-20T13:37:22.670Z (about 1 year ago)
- Topics: go, golang, gread, input, reader, scanner
- Language: Go
- Homepage:
- Size: 26.4 KB
- Stars: 14
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gread [](https://godoc.org/github.com/stevenferrer/gread)  [](https://coveralls.io/github/stevenferrer/gread?branch=main) [](https://goreportcard.com/report/github.com/stevenferrer/gread) [](https://sourcegraph.com/github.com/stevenferrer/gread?badge)
Go module for reading from anything that implements `io.Reader`
## Installing
```console
$ go get github.com/stevenferrer/gread
```
## Example
```go
package main
import (
"fmt"
"log"
"math"
"strings"
"github.com/stevenferrer/gread"
)
func main() {
s := fmt.Sprintf("%v %d %v %v %v %v\nanother line\nhello",
math.MaxInt32,
math.MaxInt64,
math.MaxUint32,
uint64(math.MaxUint64),
math.MaxFloat32,
math.MaxFloat64,
)
// Create a new `gread.Reader`
reader := gread.NewReader(strings.NewReader(s))
i32, err := reader.NextInt32()
checkErr(err)
fmt.Printf("%T is %d\n\n", i32, i32)
i64, err := reader.NextInt64()
checkErr(err)
fmt.Printf("%T is %d\n\n", i64, i64)
ui32, err := reader.NextUint32()
checkErr(err)
fmt.Printf("%T is %d\n\n", ui32, ui32)
ui64, err := reader.NextUint64()
checkErr(err)
fmt.Printf("%T is %d\n\n", ui64, ui64)
f32, err := reader.NextFloat32()
checkErr(err)
fmt.Printf("%T is %f\n\n", f32, f32)
f64, err := reader.NextFloat64()
checkErr(err)
fmt.Printf("%T is %f\n", f64, f64)
w, err := reader.NextWord()
checkErr(err)
fmt.Printf("\nword1 is %q\n", w)
w, err = reader.NextWord()
checkErr(err)
fmt.Printf("word2 is %q\n", w)
l, err := reader.NextLine()
checkErr(err)
fmt.Printf("\nline is %q\n", l)
}
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
```
## Contributing
Please feel free to improve this by **openning an issue** or **submitting a PR**