Filter sends the contents of the pipe to the function filter and produces the result. filter takes an [io.Reader] to read its input from and an [io.Writer] to write its output to, and returns an error, which will be set on the pipe. filter runs concurrently, so its goroutine will not exit until the
(filter func(io.Reader, io.Writer) error)
| 534 | // been fully read. Use [Pipe.Wait] to wait for all concurrent filters to |
| 535 | // complete. |
| 536 | func (p *Pipe) Filter(filter func(io.Reader, io.Writer) error) *Pipe { |
| 537 | if p.Error() != nil { |
| 538 | return p |
| 539 | } |
| 540 | pr, pw := io.Pipe() |
| 541 | origReader := p.Reader |
| 542 | p = p.WithReader(pr) |
| 543 | go func() { |
| 544 | defer pw.Close() |
| 545 | err := filter(origReader, pw) |
| 546 | if err != nil { |
| 547 | p.SetError(err) |
| 548 | } |
| 549 | }() |
| 550 | return p |
| 551 | } |
| 552 | |
| 553 | // FilterLine sends the contents of the pipe to the function filter, a line at |
| 554 | // a time, and produces the result. filter takes each line as a string and |