https://github.com/karrick/gotb
Go library that implements a few tail buffer abstract data types.
https://github.com/karrick/gotb
golang golang-library library text
Last synced: 16 days ago
JSON representation
Go library that implements a few tail buffer abstract data types.
- Host: GitHub
- URL: https://github.com/karrick/gotb
- Owner: karrick
- License: mit
- Created: 2018-09-27T19:39:36.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2018-09-27T19:44:59.000Z (almost 8 years ago)
- Last Synced: 2025-10-25T04:04:12.267Z (8 months ago)
- Topics: golang, golang-library, library, text
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# gotb
Implements a few tail buffer abstract data types.
## Description
Tail buffers are useful when a program needs to track the N final
elements added to a list, but not necessarily track previous elements.
```Go
// tail copies the final num lines from io.Reader to io.Writer.
func tail(num int, r io.Reader, w io.Writer) error {
cb, err := gotb.NewStrings(num)
if err != nil {
return err
}
scanner := bufio.NewScanner(r)
for scanner.Scan() {
_, _ = cb.QueueDequeue(scanner.Text())
}
if err := scanner.Err(); err != nil {
return err
}
for _, line := range cb.Drain() {
if _, err = fmt.Fprintln(w, line); err != nil {
return err
}
}
return nil
}
```