lineflushwriter

package module
v0.0.0-...-b9b8d10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2020 License: Apache-2.0 Imports: 3 Imported by: 3

README

Line Flush Writer

Simple writer, that write only complete lines (e.g. that ends with newline) to the specified backend writer.

It's guaranteed, that concurent writes to the same buffer using same mutex will be safe and writes from one routine-go will not interleave output from another.

Behavior

New     Returns Writer With Specified Values
Writer  Writes Nothing At Empty Data
Writer  Writes Single New Line
Writer  Writes Nothing If Line Is Not Complete
Writer  Writes Line If Line Is Complete
Writer  Writes Only Complete Lines
Writer  Writes Complete Lines
Writer  Bufferize Line Until Complete
Writer  Flushes Buffer On Close
Writer  Do Not Append New Line If Nothing Written
Writer  Can Ensure Newline At End Of The String On Close
Writer  Not Appends Newlines Twice On Close
Writer  Call Backend Write Only Once Per Original Call

Generated with loverage.

Benchmark

goos: linux
goarch: amd64
pkg: github.com/reconquest/lineflushwriter-go
BenchmarkWriter_Write_Parallel-12                       12476222              99.6 ns/op
BenchmarkWriter_Write_WritesLineAtomically-12            1400367               750 ns/op
PASS
ok      github.com/reconquest/lineflushwriter-go        3.315s

Reference

See reference at godoc.org.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer implements writer, that will proxy to specified `backend` writer only complete lines, e.g. that ends in newline. This writer is thread-safe.

func New

func New(
	writer io.WriteCloser,
	lock sync.Locker,
	ensureNewline bool,
) *Writer

New returns new Writer, that will proxy data to the `backend` writer, thread-safety is guaranteed via `lock`. Optionally, writer can ensure, that last line of output ends with newline, if `ensureNewline` is true.

func (*Writer) Close

func (writer *Writer) Close() error

Close flushes all remaining data and closes underlying backend writer. If `ensureNewLine` was specified and remaining data does not ends with newline, then newline will be added.

Signature matches with io.WriteCloser's Close().

func (*Writer) Write

func (writer *Writer) Write(data []byte) (int, error)

Writer writes data into Writer.

Signature matches with io.Writer's Write().

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL