Documentation
¶
Overview ¶
Package pipe is a simple stream processing library that works like Unix pipes. This library has no external dependencies and is fully asynchronous. Create a Pipe from a Reader, add some transformation functions and get the result on an io.Writer.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Filter ¶
Filter are functions to transform the stream and add them to the Pipe.
type Pipe ¶
type Pipe struct {
// Total number of bytes read at the origin of the Pipe.
TotalIn int64
// Total number of bytes written at the end of the Pipe.
TotalOut int64
// contains filtered or unexported fields
}
Pipe object
Example ¶
package main
import (
"compress/gzip"
"github.com/hyperboloide/pipe"
"io"
"log"
"os"
)
func main() {
// some Filter transformation function
var zip = func(r io.Reader, w io.Writer) error {
gzw, err := gzip.NewWriterLevel(w, gzip.BestSpeed)
if err != nil {
return err
}
defer gzw.Close()
_, err = io.Copy(gzw, r)
return err
}
// pipe input
in, err := os.Open("test.txt")
if err != nil {
log.Fatal(err)
}
defer in.Close()
// create a new pipe with a io.Reader
p := pipe.New(in)
// Pushing transformation function
p.Push(zip)
// pipe output
out, err := os.Create("test.txt.tgz")
if err != nil {
log.Fatal(err)
}
defer out.Close()
// Set pipe output io.Writer
p.To(out)
// Wait for pipe process to complete
if err := p.Exec(); err != nil {
log.Fatal(err)
}
}
func (*Pipe) Exec ¶
Exec waits for the Pipe to complete and returns an error if any of the functions failed.
func (*Pipe) Push ¶
Push appends a function to the Pipe. Note that you can add as many functions as you like at once or separatly. They will be processed in order.
func (*Pipe) Tee ¶
Tee creates a new Pipe to duplicate the stream. The stream will pass through all previously pushed functions before going through the tee Pipe. Functions pushed to the original Pipe after a call to Tee will not alter the new Tee Pipe.
func (*Pipe) ToCloser ¶
func (p *Pipe) ToCloser(w io.WriteCloser) *Pipe
ToCloser writes the ouptut of the Pipe in io.WriteCloser w and close at the end.
Source Files
¶
- pipe.go