title: How to create an AVRO file using Go?
body: <p>I'm trying to create an AVRO file using Go. So far I tried a couple of libraries and I have some code.</p>
  
  <p>The problem is that I can work with the data but don't know how to serialize it to store it. Here's the code I got from <code>github.com/hamba/avro</code> with some small modifications.</p>
  
  <pre><code>import (
    "fmt"
    "github.com/hamba/avro"
    "log"
  )
  
  type SimpleRecord struct {
          A int64  `avro:"a"`
          B string `avro:"b"`
  }
  
  func main() {
      schema, err := avro.Parse(`{
          "type": "record",
          "name": "simple",
          "namespace": "hamba",
          "fields" : [
              {"name": "a", "type": "long"},
              {"name": "b", "type": "string"}
          ]
      }`)
      if err != nil {
          log.Fatal(err)
      }
  
      in := SimpleRecord{A: 27, B: "foo"}
  
      data, err := avro.Marshal(schema, in)
      if err != nil {
          log.Fatal(err)
      }
  
      fmt.Println(data)
  }
  </code></pre>
  
  <p>This block of code prints:</p>
  
  <pre><code>[54 6 102 111 111]
  </code></pre>
  
  <p>This line corresponds to the avro encoding of the data. And it seems like this is all I need to store, but I don't know how to create the file itself.</p>
  
  <p>I tried:</p>
  
  <pre><code>mode := int(0644)
      permissions := os.FileMode(mode)
      err = ioutil.WriteFile("file.avro", data, permissions)
      if err != nil {
          log.Fatal(err)
      }
  </code></pre>
  
  <p>And it generates a file. However, when I try to read it as an AVRO file using Python fastavro library, I get the error <code>ValueError: cannot read header - is it an avro file?</code>.</p>
  
  <p>But according to the docs (<a href="https://godoc.org/github.com/hamba/avro#example-Marshal" rel="nofollow noreferrer">https://godoc.org/github.com/hamba/avro#example-Marshal</a>): "Marshal returns the Avro encoding of v." <code>Marshal(schema Schema, v interface{}) ([]byte, error)</code>, so <code>data</code> should be of type <code>[]byte</code>.</p>