Documentation ¶
Overview ¶
Package decowriter provides a writer that add a prefix and a suffix to each line.
This package is the successor of github.com/goaux/prefixwriter. Compared to prefixwriter, it is the caller's responsibility to use bufio with decowriter. decowriter uses the underlying io.Writer directly.
Example ¶
package main import ( "bufio" "fmt" "os" "github.com/goaux/decowriter" ) func main() { w := decowriter.New(bufio.NewWriter(os.Stdout), []byte(">>"), []byte("<<")) fmt.Fprint(w, "hello") fmt.Fprintln(w, " world") fmt.Fprintln(w, "hello") fmt.Fprint(w, "world") fmt.Printf("\ntotal: %d\n", w.Written()) }
Output: >>hello world<< >>hello<< >>world total: 33
Example (Readme) ¶
package main import ( "bufio" "fmt" "os" "github.com/goaux/decowriter" ) func main() { w := decowriter.New(bufio.NewWriter(os.Stdout), []byte(">>"), []byte("<<")) fmt.Fprintln(w, "This is a log message") fmt.Fprintln(w, "Another log message") }
Output: >>This is a log message<< >>Another log message<<
Index ¶
Examples ¶
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 is the writer that add a prefix and a suffix to each line. It implements the io.Writer interface.
Writer defines a line as a sequence of zero or more non-'\n' bytes followed by a '\n'. A prefix is written before one or more bytes of a line. A suffix is written just before the trailing '\n'. A string without a trailing '\n' is not recognized as a line, so no suffix is written. This allows you to split a single line write across two Write calls.
func (*Writer) Flush ¶
Flush calls the Flush method of underlying writer if it has, otherwise returns nil.
func (*Writer) Write ¶
Write implements the io.Writer interface. It writes the given byte slice to the underlying writer, adding the prefix at the beginning of each line, adding the suffix at the end of each line.
The returned int n represents the number of bytes from the input slice p that were processed, not including any added prefixes nor suffixes. This means that n <= len(p), even though the actual number of bytes written to the underlying writer may be larger due to the added prefixes and suffixes.
If p contains no data (len(p) == 0), Write will not perform any operation and will return n = 0 and a nil error.
An error is returned if the underlying writer returns an error, or if the Write operation cannot be completed fully.
If one or more bytes are written to the underlying writer and the processing is completed normally, and if the underlying writer has `Flush() error` method, Flush is called once at the end.