github.com/hamba/avro@v1.8.0/ocf/example_test.go (about) 1 package ocf_test 2 3 import ( 4 "log" 5 "os" 6 7 "github.com/hamba/avro/ocf" 8 ) 9 10 func ExampleNewDecoder() { 11 type SimpleRecord struct { 12 A int64 `avro:"a"` 13 B string `avro:"b"` 14 } 15 16 f, err := os.Open("/your/avro/file.avro") 17 if err != nil { 18 log.Fatal(err) 19 } 20 defer f.Close() 21 22 dec, err := ocf.NewDecoder(f) 23 if err != nil { 24 log.Fatal(err) 25 } 26 27 for dec.HasNext() { 28 var record SimpleRecord 29 err = dec.Decode(&record) 30 if err != nil { 31 log.Fatal(err) 32 } 33 34 // Do something with the data 35 } 36 37 if dec.Error() != nil { 38 log.Fatal(err) 39 } 40 } 41 42 func ExampleNewEncoder() { 43 schema := `{ 44 "type": "record", 45 "name": "simple", 46 "namespace": "org.hamba.avro", 47 "fields" : [ 48 {"name": "a", "type": "long"}, 49 {"name": "b", "type": "string"} 50 ] 51 }` 52 53 type SimpleRecord struct { 54 A int64 `avro:"a"` 55 B string `avro:"b"` 56 } 57 58 f, err := os.Open("/your/avro/file.avro") 59 if err != nil { 60 log.Fatal(err) 61 } 62 defer f.Close() 63 64 enc, err := ocf.NewEncoder(schema, f) 65 if err != nil { 66 log.Fatal(err) 67 } 68 69 var record SimpleRecord 70 err = enc.Encode(record) 71 if err != nil { 72 log.Fatal(err) 73 } 74 75 if err := enc.Flush(); err != nil { 76 log.Fatal(err) 77 } 78 79 if err := f.Sync(); err != nil { 80 log.Fatal(err) 81 } 82 }