Answer the question
In order to leave comments, you need to log in
How to apply multiple filters to the incoming stream?
There is a code that processes the input stream stdin and outputs to stdout.
var input io.ReadCloser
reader := bufio.NewReader(input)
for {
in, _, err := reader.ReadLine()
if err != nil && err == io.EOF {
break
}
in = append(in, []byte("\n")...)
if _, err := w.Write(in); err != nil {
log.Fatalln(err)
}
}
Filter(io.Reader, io.Writer) error
input
a processed stream after the last filter is applied? applyFilters(input io.Reader) (io.Reader, error)
. Then I could writefilteredInput, err = applyFilters(input)
reader := bufio.NewReader(filteredInput)
io.Pipe
? If so, how?
Answer the question
In order to leave comments, you need to log in
Another example is how you can
type FReader struct {
reader io.Reader
filter func([]byte) (int, error)
}
func NewFReader(r io.Reader) *FReader {
return &FReader{r, func(p []byte) (int, error) { return len(p), nil }}
}
func (fr *FReader) Filter(filter func([]byte) (int, error)) *FReader {
return &FReader{fr, filter}
}
func (fr *FReader) Read(p []byte) (n int, err error) {
n, err = fr.reader.Read(p)
if err == nil && fr.filter != nil {
n, err = fr.filter(p)
}
return
}
chainreader := NewFReader(myreader).Filter(myfilter1).Filter(myfilter2).Filter(myfilter3)
https://play.golang.org/p/rakSV5kgqR9
having an array of filtersNo need to put filters in an array)
process the incoming stream so that there are no blockings and input contains the processed stream after applying the last filter?Channels are here to help.
That is, by analogy with using pipe on the command line.Yes, yes, the channels will help you) They work like this.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question