p.s. If you changed the above code to use strings rather than []byte 
it would run many times slower due to the cost of allocation.

On Saturday, 7 May 2022 at 22:49:08 UTC+1 Amnon wrote:

> How about something like 
>
> func grep(pat []byte, r io.Reader, w io.Writer) error {
>     scanner := bufio.NewScanner(r)
>     for scanner.Scan() {
>         if (bytes.Contains(scanner.Bytes(), pat)) {
>             w.Write(scanner.Bytes())
>         }
>     }
>
>     return scanner.Err()
> }
>
> and for extra speed, just allocate a bigger buffer to the scanner...
> On Saturday, 7 May 2022 at 21:46:33 UTC+1 Jan Mercl wrote:
>
>> On Sat, May 7, 2022 at 10:24 PM Constantine Vassilev <ths...@gmail.com> 
>> wrote:
>>
>> > I need to write a program that reads STDIN and should output every line 
>> that contains a search word "test" to STDOUT.
>>
>> Piping the data through grep(1) would be my first option.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/ee516197-dd8c-4af4-beb7-fa5c503e66b6n%40googlegroups.com.

Reply via email to