Standard library / testing/iotest
testing/iotest.TruncateWriter()
Silently discard bytes beyond the configured limit while reporting successful writes; useful for simulating a misleading sink. Expected stdout: '6 true abc\n'.
Formal Syntax & Structure
go syntaxgo
func TruncateWriter(w io.Writer, n int64) io.WriterParameters & Attributes
| Name | Type | Description |
|---|---|---|
| w | io.Writer | Underlying writer. |
| n | int64 | Byte limit or numeric measurement, depending on the API. |
Return Value
io.Writer
Result of the operation described above.
Working Example
Example Snippetgo
package main
import ( "bytes" "fmt" "testing/iotest")
func main() { var b bytes.Buffer w := iotest.TruncateWriter(&b, 3) n, err := w.Write([]byte("abcdef")) fmt.Println(n, err == nil, b.String())}