Skip to content

Commit

Permalink
README updates
Browse files Browse the repository at this point in the history
  • Loading branch information
neilotoole committed Jan 24, 2024
1 parent 85b6980 commit fef30d0
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,42 +56,42 @@ of `stdin` to `stdout` and `stderr`, and prints the number of bytes read.
package main

import (
"context"
"errors"
"fmt"
"io"
"os"
"context"
"errors"
"fmt"
"io"
"os"

"github.com/neilotoole/streamcache"
"github.com/neilotoole/streamcache"
)

// Write stdin to both stdout and stderr.
// Some error handling omitted for brevity.
func main() {
ctx := context.Background()
stream := streamcache.New(os.Stdin)

r1 := stream.NewReader(ctx)
go func() {
defer r1.Close()
io.Copy(os.Stdout, r1)
}()

r2 := stream.NewReader(ctx)
go func() {
defer r2.Close()
io.Copy(os.Stderr, r2)
}()

stream.Seal() // Indicate that there'll be no more readers...
<-stream.Done() // Receives when all readers are closed.

if err := stream.Err(); err != nil && !errors.Is(err, io.EOF) {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}

fmt.Fprintf(os.Stdout, "Read %d bytes from stdin\n", stream.Size())
ctx := context.Background()
stream := streamcache.New(os.Stdin)

r1 := stream.NewReader(ctx)
go func() {
defer r1.Close()
io.Copy(os.Stdout, r1)
}()

r2 := stream.NewReader(ctx)
go func() {
defer r2.Close()
io.Copy(os.Stderr, r2)
}()
stream.Seal() // Indicate that there'll be no more readers...
<-stream.Done() // Receives when all readers are closed.

if err := stream.Err(); err != nil && !errors.Is(err, io.EOF) {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}

fmt.Fprintf(os.Stdout, "Read %d bytes from stdin\n", stream.Size())
}
```

Expand Down

0 comments on commit fef30d0

Please sign in to comment.