Skip to content

Instantly share code, notes, and snippets.

@karrick
Created September 25, 2015 19:37
Show Gist options
  • Save karrick/c71686fb5bb6e9a17846 to your computer and use it in GitHub Desktop.
Save karrick/c71686fb5bb6e9a17846 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"log"
"github.com/linkedin/goavro"
)
var (
schema = `
{
"name" : "event",
"type" : "record",
"namespace" : "mobi.bigtree.avro",
"fields" : [
{ "type": "string", "name" : "string" },
{ "type": ["null", "string"], "name" : "nil_or_string", "default": null }
]
}
`
)
func main() {
codec, err := goavro.NewCodec(schema)
if err != nil {
log.Fatalln("Failed to create codec.", err)
}
record, err := goavro.NewRecord(goavro.RecordSchema(schema))
if err != nil {
log.Fatalln("Failed to create record.", err)
}
record.Set("string", "this field cannnot be nil.")
// record.Set("nil_or_string", nil) // or just leave it unset(then it should be nil)
buf := new(bytes.Buffer)
if err = codec.Encode(buf, record); err != nil {
log.Fatalln("Failed to encode.", err)
}
decode, err := codec.Decode(buf)
if err != nil {
log.Fatalln("Failed to decode.", err)
}
log.Println(decode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment