Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/evg4b/linebyline
Thread-safe io.WriterCloser allowing you to write from different sources in one line by line.
https://github.com/evg4b/linebyline
go golang iowriter
Last synced: 15 days ago
JSON representation
Thread-safe io.WriterCloser allowing you to write from different sources in one line by line.
- Host: GitHub
- URL: https://github.com/evg4b/linebyline
- Owner: evg4b
- License: mit
- Created: 2021-08-18T21:54:53.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-20T01:02:01.000Z (10 months ago)
- Last Synced: 2024-01-20T01:59:51.043Z (10 months ago)
- Topics: go, golang, iowriter
- Language: Go
- Homepage:
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Line by line writer [![Go Report Card](https://goreportcard.com/badge/github.com/evg4b/linebyline)](https://goreportcard.com/report/github.com/evg4b/linebyline)
Thread-safe `io.WriterCloser` allowing you to write from different sources in one line by line.
Writing to the source occurs only after the `end of the line` or the `closing of the generated io.WriterCloser`.## Example:
``` GO
package mainimport (
"fmt"
"os"
"sync"
"time""github.com/evg4b/linebyline"
)func main() {
wg := sync.WaitGroup{}
wr1 := linebyline.NewByLineWriter(
linebyline.WithOutWriter(os.Stdout),
)
wr2 := linebyline.NewByLineWriter(
linebyline.WithOutWriter(os.Stdout),
)wg.Add(2)
go func() {
defer wg.Done()
for i := 0; i < 50; i++ {
fmt.Fprintf(wr1, "[#1] line %d ", i)
// do something else ...
time.Sleep(10 * time.Millisecond)fmt.Fprintln(wr1, "first writer")
}
}()go func() {
defer wg.Done()
for i := 0; i < 50; i++ {
fmt.Fprintf(wr2, "[#2] line %d ", i)
// do something else ...
time.Sleep(20 * time.Millisecond)fmt.Fprintln(wr2, "second writer")
}
}()wg.Wait()
err := w1.Close()
if err != nil {
panic(err)
}err := w2.Close()
if err != nil {
panic(err)
}
}
```Output:
```
[#1] line 0 first writer
[#2] line 0 second writer
[#1] line 1 first writer
[#1] line 2 first writer
[#2] line 1 second writer
[#1] line 3 first writer
[#1] line 4 first writer
[#1] line 5 first writer
[#2] line 2 second writer
[#1] line 6 first writer
[#1] line 7 first writer
...
```### For not safe writers:
``` GO
var writer bytes.Buffer
safeWriter := linebyline.NewSafeWriter(&writer)wr1 := linebyline.NewByLineWriter(
linebyline.WithOutWriter(safeWriter),
)
defer wr1.Close()wr2 := linebyline.NewByLineWriter(
linebyline.WithOutWriter(safeWriter),
)
defer wr2.Close()fmt.Fprintln(wr1, "second writer")
fmt.Fprintln(wr2, "second writer")
// do something else ...```
### Use flush function
``` GO
wr1 := linebyline.NewByLineWriter(
linebyline.WithFlushFunc(func(bytes []byte) error {
print(string(bytes))
return nil
}),
)
defer wr1.Close()wr2 := linebyline.NewByLineWriter(
linebyline.WithFlushFunc(func(bytes []byte) error {
print(string(bytes))
return nil
}),
)
defer wr2.Close()fmt.Fprintln(wr1, "second writer")
fmt.Fprintln(wr2, "second writer")
// do something else ...
```### Use custom end rune
``` GO
wr1 := linebyline.NewByLineWriter(
linebyline.WithOutWriter(os.Stdout),
linebyline.WithEndRune('\t'),
)
defer wr1.Close()wr2 := linebyline.NewByLineWriter(
linebyline.WithOutWriter(os.Stdout),
linebyline.WithEndRune('\t'),
)
defer wr2.Close()fmt.Fprint(wr1, "second writer\t")
fmt.Fprint(wr2, "second writer\t")
// do something else ...
```### Use custom end rune
``` GO
wr1 := linebyline.NewByLineWriter(
linebyline.WithOutWriter(os.Stdout),
linebyline.OmitNewLineRune(),
)
defer wr1.Close()wr2 := linebyline.NewByLineWriter(
linebyline.WithOutWriter(os.Stdout),
linebyline.OmitNewLineRune(),
)
defer wr2.Close()fmt.Fprintln(wr1, "second writer")
fmt.Fprintln(wr2, "second writer")
// do something else ...
```