Skip to content

Instantly share code, notes, and snippets.

@lixin9311
Created September 25, 2015 05:00
Show Gist options
  • Save lixin9311/6e117363d1b709b8411e to your computer and use it in GitHub Desktop.
Save lixin9311/6e117363d1b709b8411e to your computer and use it in GitHub Desktop.
package main
import (
"github.com/linkedin/goavro"
"log"
"bytes"
)
var (
schema = `
{
"name" : "event",
"type" : "record",
"namespace" : "mobi.bigtree.avro",
"fields" : [
{ "type": "string", "name" : "string" },
{ "type": ["string", "null"], "name" : "nil_or_string" }
]
}
`
)
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