github.com/hamba/avro@v1.8.0/example_simple_test.go (about)

     1  package avro_test
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hamba/avro"
     8  )
     9  
    10  var Schema = `{
    11  		"type": "record",
    12  		"name": "simple",
    13  		"namespace": "org.hamba.avro",
    14  		"fields" : [
    15  			{"name": "a", "type": "long"},
    16  			{"name": "b", "type": "string"}
    17  		]
    18  	}`
    19  
    20  type SimpleRecord struct {
    21  	A int64  `avro:"a"`
    22  	B string `avro:"b"`
    23  }
    24  
    25  func Example_usage() {
    26  	schema, err := avro.Parse(Schema)
    27  	if err != nil {
    28  		log.Fatal(err)
    29  	}
    30  
    31  	in := SimpleRecord{A: 27, B: "foo"}
    32  
    33  	data, err := avro.Marshal(schema, in)
    34  	if err != nil {
    35  		log.Fatal(err)
    36  	}
    37  
    38  	fmt.Printf("%+v\n", data)
    39  
    40  	out := SimpleRecord{}
    41  	err = avro.Unmarshal(schema, data, &out)
    42  	if err != nil {
    43  		log.Fatal(err)
    44  	}
    45  
    46  	fmt.Printf("%+v\n", out)
    47  
    48  	//Output: [54 6 102 111 111]
    49  	// {A:27 B:foo}
    50  }