Standard library / testing/iotest

testing/iotest.NewWriteLogger()

Log written bytes in hexadecimal; use synthetic payloads to avoid leaking secrets. Expected stdout: '2 true ab\n'.

Formal Syntax & Structure

go syntaxgo
func NewWriteLogger(prefix string, w io.Writer) io.Writer

Parameters & Attributes

NameTypeDescription
prefixstringText prepended to diagnostic logs.
wio.WriterUnderlying writer.

Return Value

io.Writer

Result of the operation described above.

Working Example

Example Snippetgo
package main
import (  "bytes"  "fmt"  "log"  "testing/iotest")
func main() {  log.SetFlags(0)  var b bytes.Buffer  w := iotest.NewWriteLogger("write", &b)  n, err := w.Write([]byte("ab"))  fmt.Println(n, err == nil, b.String())}
Go Reference