• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..24-Mar-2022-

README.mdH A D24-Mar-20221.1 KiB4330

bufpipe.goH A D24-Mar-20223.4 KiB12978

doc.goH A D24-Mar-202282 31

README.md

1# bufpipe: Buffered Pipe
2
3[![CircleCI](https://img.shields.io/circleci/build/github/acomagu/bufpipe.svg?style=flat-square)](https://circleci.com/gh/acomagu/bufpipe) [![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/acomagu/bufpipe)
4
5The buffered version of io.Pipe. It's safe for concurrent use.
6
7## How does it differ from io.Pipe?
8
9Writes never block because the pipe has variable-sized buffer.
10
11```Go
12r, w := bufpipe.New(nil)
13io.WriteString(w, "abc") // No blocking.
14io.WriteString(w, "def") // No blocking, too.
15w.Close()
16io.Copy(os.Stdout, r)
17// Output: abcdef
18```
19
20[Playground](https://play.golang.org/p/PdyBAS3pVob)
21
22## How does it differ from bytes.Buffer?
23
24Reads block if the internal buffer is empty until the writer is closed.
25
26```Go
27r, w := bufpipe.New(nil)
28
29done := make(chan struct{})
30go func() {
31	io.Copy(os.Stdout, r) // The reads block until the writer is closed.
32	done <- struct{}{}
33}()
34
35io.WriteString(w, "abc")
36io.WriteString(w, "def")
37w.Close()
38<-done
39// Output: abcdef
40```
41
42[Playground](https://play.golang.org/p/UppmyLeRgX6)
43