title: How do I decode an Avro message that was encoded using goavro.OCFWriter?
body: <p>I am trying to encode a message with its schema using Avro's Object Container File format and <a href="https://github.com/linkedin/goavro" rel="nofollow noreferrer">https://github.com/linkedin/goavro</a>. I think I'm encoding an object correctly. But when I attempt to decode it, the message appears to be empty. The following code demonstrates the problem.</p>
  <pre><code>package main
  
  import (
      &quot;bytes&quot;
      &quot;errors&quot;
      &quot;fmt&quot;
  
      &quot;github.com/linkedin/goavro/v2&quot;
  )
  
  type Animal struct {
      Name string
  }
  
  func serialize(schema string, msg interface{}) ([]byte, error) {
      buf := bytes.NewBuffer(nil)
  
      ocfw, err := goavro.NewOCFWriter(goavro.OCFConfig{
          W:      buf,
          Schema: schema,
      })
      if err != nil {
          return nil, err
      }
  
      in, ok := msg.(Animal)
      if !ok {
          return nil, errors.New(&quot;not an animal&quot;)
      }
  
      out := map[string]interface{}{}
  
      if in.Name == &quot;&quot; {
          out[&quot;Name&quot;] = goavro.Union(&quot;null&quot;, nil)
      } else {
          out[&quot;Name&quot;] = goavro.Union(&quot;string&quot;, in.Name)
      }
  
      w := []map[string]interface{}{out}
      if err := ocfw.Append(w); err != nil {
          return nil, err
      }
  
      return buf.Bytes(), nil
  }
  
  func deserialize(b []byte) (interface{}, error) {
      var msgs []interface{}
  
      ocfr, err := goavro.NewOCFReader(bytes.NewReader(b))
      if err != nil {
          return nil, err
      }
  
      for ocfr.Scan() {
          v, err := ocfr.Read()
          if err != nil {
              return nil, err
          }
          fmt.Println(&quot;read:&quot;, v)
  
          //TODO: Once the value is being read as not nil, decode it into out.
          out := Animal{}
  
          msgs = append(msgs, out)
      }
      if err := ocfr.Err(); err != nil {
          return nil, err
      }
  
      return msgs, nil
  }
  
  func main() {
      schema := `{&quot;type&quot;:&quot;record&quot;,&quot;name&quot;:&quot;Animal&quot;,&quot;fields&quot;:[{&quot;name&quot;:&quot;name&quot;,&quot;type&quot;:[&quot;null&quot;,&quot;string&quot;],&quot;default&quot;:null}]}`
      msg := Animal{
          Name: &quot;Tiger&quot;,
      }
  
      encoded, err := serialize(schema, msg)
      if err != nil {
          panic(err)
      }
  
      fmt.Println(&quot;encoded:&quot;, encoded)
  
      decoded, err := deserialize(encoded)
      if err != nil {
          panic(err)
      }
  
      fmt.Println(&quot;decoded:&quot;, decoded)
  }
  </code></pre>
  <p>The output is:</p>
  <pre><code>encoded: [79 98 106 1 4 22 97 118 114 111 46 115 99 104 101 109 97 200 1 123 34 116 121 112 101 34 58 34 114 101 99 111 114 100 34 44 34 110 97 109 101 34 58 34 65 110 105 109 97 108 34 44 34 102 105 101 108 100 115 34 58 91 123 34 110 97 109 101 34 58 34 110 97 109 101 34 44 34 116 121 112 101 34 58 91 34 110 117 108 108 34 44 34 115 116 114 105 110 103 34 93 44 34 100 101 102 97 117 108 116 34 58 110 117 108 108 125 93 125 20 97 118 114 111 46 99 111 100 101 99 8 110 117 108 108 0 85 252 58 132 245 55 172 159 242 74 105 116 221 238 89 247 2 2 0 85 252 58 132 245 55 172 159 242 74 105 116 221 238 89 247]
  read: map[name:&lt;nil&gt;]
  decoded: [{}]
  </code></pre>
  <p>I would expect the line that starts with <code>read:</code> to be <code>map[name:Tiger]</code>. Where am I going wrong?</p>