github.com/aacfactory/avro@v1.2.12/README.md (about)

     1  # avro
     2  avro for go
     3  
     4  ## Install
     5  ```shell
     6  go get github.com/aacfactory/avro
     7  ```
     8  
     9  ## Usage
    10  Set `avro` tag.
    11  ```go
    12  type Anonymous struct {
    13  	Anonymous string `avro:"anonymous"`
    14  }
    15  
    16  type Bar struct {
    17  	String string `avro:"string"`
    18  	Next   *Bar   `avro:"next"`
    19  }
    20  
    21  type Foo struct {
    22  	Anonymous
    23  	String  string         `avro:"string"`
    24  	Boolean bool           `avro:"boolean"`
    25  	Int     int            `avro:"int"`
    26  	Long    int64          `avro:"long"`
    27  	Float   float32        `avro:"float"`
    28  	Double  float64        `avro:"double"`
    29  	Uint    uint64         `avro:"uint"`
    30  	Time    time.Time      `avro:"time"`
    31  	Dur     time.Duration  `avro:"dur"`
    32  	Byte    byte           `avro:"byte"`
    33  	Bytes   []byte         `avro:"bytes"`
    34  	Bar     Bar            `avro:"bar"`
    35  	Baz     *Bar           `avro:"baz"`
    36  	Bars    []Bar          `avro:"bars"`
    37  	Map     map[string]Bar `avro:"map"`
    38  }
    39  
    40  ```
    41  Marshal.
    42  ```go
    43      foo := Foo{
    44  		Anonymous: Anonymous{
    45  			Anonymous: "Anonymous",
    46  		},
    47  		String:  "foo",
    48  		Boolean: true,
    49  		Int:     1,
    50  		Long:    2,
    51  		Float:   3.3,
    52  		Double:  4.4,
    53  		Uint:    uint64(5),
    54  		Time:    time.Now(),
    55  		Dur:     10 * time.Hour,
    56  		Byte:    'B',
    57  		Bytes:   []byte("bytes"),
    58  		Bar: Bar{
    59  			String: "bar",
    60  			Next: &Bar{
    61  				String: "Bar-Next",
    62  				Next:   nil,
    63  			},
    64  		},
    65  		Baz:  nil,
    66  		Bars: []Bar{{String: "bar-1"}},
    67  		Map:  map[string]Bar{"bar2": {String: "bar-2"}},
    68  	}
    69  
    70  	p, encodeErr := base.Marshal(s, foo)
    71  ```
    72  Unmarshal
    73  ```go
    74      r := Foo{}
    75  	decodeErr := base.Unmarshal(s, p, &r)
    76  	if decodeErr != nil {
    77  		t.Error(decodeErr)
    78  		return
    79  	}
    80  ```
    81  
    82  ## Note:
    83  `interface` is not supported.
    84  ## Benchmark
    85  avro
    86  ```
    87  BenchmarkAvro-20         1000000              1054 ns/op             626 B/op	9 allocs/op
    88  ```
    89  encoding/json
    90  ```
    91  BenchmarkJson-20          260964              4495 ns/op            1218 B/op   29 allocs/op
    92  ```
    93  json-iterator/go
    94  ```
    95  BenchmarkJson-20          591304              1953 ns/op             889 B/op   19 allocs/op
    96  ```