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.WriterParameters & Attributes
| Name | Type | Description |
|---|---|---|
| prefix | string | Text prepended to diagnostic logs. |
| w | io.Writer | Underlying 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())}