github.com/grailbio/base@v0.0.11/recordio/example_basic_test.go (about) 1 package recordio_test 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 8 "github.com/grailbio/base/recordio" 9 "github.com/grailbio/base/recordio/recordioflate" 10 ) 11 12 func init() { 13 recordioflate.Init() 14 } 15 16 func doWrite(out io.Writer) { 17 wr := recordio.NewWriter(out, recordio.WriterOpts{ 18 Transformers: []string{"flate"}, 19 Marshal: func(scratch []byte, v interface{}) ([]byte, error) { return []byte(v.(string)), nil }, 20 }) 21 wr.Append("Item0") 22 wr.Append("Item1") 23 if err := wr.Finish(); err != nil { 24 panic(err) 25 } 26 } 27 28 func doRead(in io.ReadSeeker) { 29 r := recordio.NewScanner(in, recordio.ScannerOpts{ 30 Unmarshal: func(data []byte) (interface{}, error) { return string(data), nil }, 31 }) 32 for r.Scan() { 33 fmt.Printf("Item: %s\n", r.Get().(string)) 34 } 35 if err := r.Err(); err != nil { 36 panic(err) 37 } 38 } 39 40 // Example_basic demonstrates basic reads, writes, and flate complession. 41 func Example_basic() { 42 buf := &bytes.Buffer{} 43 doWrite(buf) 44 doRead(bytes.NewReader(buf.Bytes())) 45 // Output: 46 // Item: Item0 47 // Item: Item1 48 }