You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
762 B
Go
33 lines
762 B
Go
package main
|
|
|
|
import "io"
|
|
|
|
type prefixingWriter struct {
|
|
prefix []byte
|
|
writer io.Writer
|
|
}
|
|
|
|
func (pw *prefixingWriter) Write(p []byte) (int, error) {
|
|
if len(p) == 0 {
|
|
return 0, nil
|
|
}
|
|
|
|
// Perform an "atomic" write of a prefix and p to make sure that it doesn't interleave
|
|
// sub-line when used concurrently with io.PipeWrite.
|
|
n, err := pw.writer.Write(append(pw.prefix, p...))
|
|
if n > len(p) {
|
|
// To comply with the io.Writer interface requirements we must
|
|
// return a number of bytes written from p (0 <= n <= len(p)),
|
|
// so we are ignoring the length of the prefix here.
|
|
return len(p), err
|
|
}
|
|
return n, err
|
|
}
|
|
|
|
func prefixWriter(prefix string, sink io.Writer) io.Writer {
|
|
return &prefixingWriter{
|
|
prefix: []byte(prefix),
|
|
writer: sink,
|
|
}
|
|
}
|