Standard library / testing/iotest

testing/iotest.ErrReader()

Always return zero bytes and the specified error. Expected stdout: '0 broken\n'.

Formal Syntax & Structure

go syntaxgo
func ErrReader(err error) io.Reader

Parameters & Attributes

NameTypeDescription
errerrorError to inject into reads.

Return Value

io.Reader

Result of the operation described above.

Working Example

Example Snippetgo
package main
import (  "errors"  "fmt"  "testing/iotest")
func main() {  r := iotest.ErrReader(errors.New("broken"))  n, err := r.Read(make([]byte, 4))  fmt.Println(n, err)}
Go Reference