Documentation
¶
Overview ¶
Package iostream contains various io.Reader and io.Writer implementations.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ScanWriterMaxBufferSize = 1024 * 1024
ScanWriterMaxBufferSize is the maximum size of the ScanWriter buffer. If the buffer is full before the delimiter is encountered, the buffer contents are flushed like the delimiter was encountered.
Functions ¶
func NewScanWriter ¶
func NewScanWriter(fn CallbackFn) io.WriteCloser
NewScanWriter returns a new ScanWriter.
Types ¶
type ByteCounter ¶
type ByteCounter struct {
// contains filtered or unexported fields
}
ByteCounter is a simple io.Writer that counts the number of bytes written to it, to be used in conjunction with io.MultiWriter / io.TeeReader.
func (*ByteCounter) Count ¶
func (bc *ByteCounter) Count() int64
Count returns the number of bytes written to the ByteCounter.
type CallbackFn ¶
type CallbackFn func(string)
CallbackFn is a function that takes a string as an argument and returns nothing.
type ScanWriter ¶
type ScanWriter struct {
// contains filtered or unexported fields
}
ScanWriter is an io.WriteCloser wrapper for bufio.Scanner. Instead of calling scanner.Scan() like in bufio.Scanner, you write to it and it calls the given callback function with the contents of the internal buffer every time it encounters the given delimiter.
You must call Close() to flush the remaining buffer contents to the scanner.
Example ¶
package main
import (
"fmt"
"io"
"strings"
"github.com/k0sproject/rig/v2/iostream"
)
func main() {
contentReader := strings.NewReader("Hello, World!\nHow are you today?\nNice to meet you.\n")
i := 1
sw := iostream.NewScanWriter(func(row string) {
fmt.Println(i, row)
i++
})
defer sw.Close()
_, _ = io.Copy(sw, contentReader)
}
Output: 1 Hello, World! 2 How are you today? 3 Nice to meet you.
func (*ScanWriter) Close ¶
func (w *ScanWriter) Close() error
Close closes the writer and the underlying pipe. It returns the last error encountered by the scanner.
func (*ScanWriter) CloseWithError ¶
func (w *ScanWriter) CloseWithError(reason error) error
CloseWithError closes the underlying pipe with an error.
func (*ScanWriter) Err ¶
func (w *ScanWriter) Err() error
Err returns the last error encountered by the scanner.
func (*ScanWriter) Split ¶
func (w *ScanWriter) Split(split bufio.SplitFunc)
Split sets the split function for the scanner. see bufio.Scanner https://pkg.go.dev/bufio#Scanner
func (*ScanWriter) Text ¶
func (w *ScanWriter) Text() string
Text returns the most recent token generated by a call to Scan as a newly allocated string holding its bytes.