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 8d35633 commit 85b6980
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 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)
}()
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.
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)
}
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())
fmt.Fprintf(os.Stdout, "Read %d bytes from stdin\n", stream.Size())
}
```

Expand Down

0 comments on commit 85b6980

Please sign in to comment.