Skip to content
Generate Go code to serialize and deserialize Avro schemas
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
compiler Fix off-by-one in loop jumps Mar 20, 2019
container Make output clearer and fix offset for byte decoding Feb 13, 2019
example Rename methods and structs to make godocs clearer Feb 13, 2019
gogen-avro Support globbing input paths Apr 8, 2019
test Support globbing input paths Apr 8, 2019
vm Make int and long deserializers aware of ByteReaders Apr 9, 2019
.gitignore Initial commit Mar 20, 2016
LICENSE Initial commit Mar 30, 2016 Release script renames imports in container tests Nov 8, 2018 Update import paths May 1, 2018


Build Status MIT licensed Version 5.3.0

Generates type-safe Go code based on your Avro schemas, including serializers and deserializers that support Avro's schema evolution rules.

Table of contents


gogen-avro has two parts: a tool which you install on your system (usually on your GOPATH) to generate code, and a runtime library that gets imported.

To install the gogen-avro executable to $GOPATH/bin/ and generate structs, first download the repository:

go get -d

Then run:

go install

We recommend pinning a specific SHA of the gogen-avro tool when you compile your schemas with a tool like This will ensure your builds are repeatable.

For the library imports, you should manage the dependency on this repo using Godep or a similar tool, like any other library.


To generate Go source files from one or more Avro schema files, run:

gogen-avro [--package=<package name>] <output directory> <avro schema files>

You can also use a go:generate directive in a source file (example):

//go:generate $GOPATH/bin/gogen-avro . primitives.avsc

Note: If you want to parse multiple .avsc files into a single Go package (a single folder), make sure you put them all in one line. gogen-avro produces a file, primitive.go, that will be overwritten if you run it multiple times with different .avsc files and the same output folder.

Generated Methods

For each record in the provided schemas, gogen-avro will produce a struct, and the following methods:


A constructor to create a new record struct, with no values set.

New<RecordType>Writer(writer io.Writer, codec container.Codec, recordsPerBlock int64) (*container.Writer, error)

Creates a new container.Writer which writes generated structs to writer with Avro OCF format. This is the method you want if you're writing Avro to files. codec supports Identity, Deflate and Snappy encodings per the Avro spec.

New<RecordType>Reader(reader io.Reader) (<RecordTypeReader>, error)

Creates a new <RecordTypeReader> which reads data in the Avro OCF format into generated structs. This is the method you want if you're reading Avro data from files. It will handle the codec and schema evolution for you based on the OCF headers and the reader schema used to generate the structs.

<RecordType>.Serialize(io.Writer) error

Write the contents of the struct into the given io.Writer in the Avro binary format, with no Avro Object Container File (OCF) framing.

Deserialize<RecordType>(io.Reader) (<RecordType>, error)

Read Avro data from the given io.Reader and deserialize it into the generated struct. This assumes the schema used to write the data is identical to the schema used to generate the struct. This method assumes there's no OCF framing. This method is also slow because it re-compiles the bytecode for your type every time - if you need performance you should call compiler.Compile once and then vm.Eval for each record.

Working with Object Container Files (OCF)

An example of how to write a container file can be found in example/container/example.go .

Godocs for the container package


The example directory contains simple example projects with an Avro schema. Once you've installed gogen-avro on your GOPATH, you can install the example projects:

# Build the Go source files from the Avro schema using the generate directive
go generate

# Install the example projects on the gopath
go install
go install


Gogen-avro converts field and type names to be valid, public Go names by following a few simple steps:

  • removing leading underscore characters (_)
  • upper-casing the first letter of the name

This minimizes the risk that two fields with different Avro names will have the same Go name.

Gogen-avro respects namespaces and aliases when resolving type names. However, generated files will all be placed directly into the package specified by the user. This may cause issues in rare cases where two types have different namespaces but the same name.

Type Conversion

Gogen-avro produces a Go struct which reflects the structure of your Avro schema. Most Go types map neatly onto Avro types:

Avro Type Go Type Notes
null interface{} This is just a placeholder, nothing is encoded/decoded
boolean bool
int, long int32,int64
float, double float32, float64
bytes []byte
string string
enum custom type Generates a type with a constant for each symbol
array []
map custom struct Generates a struct with a field M, M has the type map[string]
fixed []byte Fixed fields are given a custom type, which is an alias for an appropriately sized byte array
union custom struc Unions are handled as a struct with one field per possible type, and an enum field to dictate which field to read

union is more complicated than primitive types. We generate a struct and enum whose name is uniquely determined by the types in the union. For a field whose type is ["null", "int"] we generate the following:

type UnionNullInt struct {
	// All the possible types the union could take on
	Null               interface{}
	Int                int32
	// Which field actually has data in it - defaults to the first type in the list, "null"
	UnionType          UnionNullTypeEnum

type UnionNullIntTypeEnum int

const (
	UnionNullIntTypeEnumNull            UnionNullIntTypeEnum = 0
	UnionNullIntTypeEnumInt             UnionNullIntTypeEnum = 1


Until version 6.0 this project used for versioning of both the code generation tool and library. Older versions are still available on


  • Support for writing object container files is no longer experimental
  • container package now works with the generated code for any record type
  • Aliases and namespaces are now used properly to resolve types
  • Record structs expose a Schema method which includes metadata from the schema definition


  • Experimental support for writing object container files
  • Improved variable and type names
  • Support for custom package names as a command line argument


  • Bug fixes for arrays and maps with record members
  • Refactored internals significantly


  • Initial release
  • No longer supported - no more bugfixes are being backported

Reporting Issues

When reporting issues, please include your reader and writer schemas, and the output from the compiler logs by adding this to one of your source files:

import (

func init() {
	compiler.LoggingEnabled = true

The logs will be printed on stdout.


Thanks to LinkedIn's goavro library, for providing the encoders for primitives.

You can’t perform that action at this time.