github.com/niubaoshu/gotiny@v0.0.3/README.md (about)

     1  ## <font color="#FF4500" >gotiny 尚处于开发的早期阶段,暂不建议使用。</font>
     2  
     3  # gotiny   [![Build status][travis-img]][travis-url] [![License][license-img]][license-url] [![GoDoc][doc-img]][doc-url] [![Go Report Card](https://goreportcard.com/badge/github.com/niubaoshu/gotiny)](https://goreportcard.com/report/github.com/niubaoshu/gotiny)
     4  gotiny是一个注重效率的go语言序列化库。gotiny通过预先生成编码机和减少使用reflect库等方式来提高效率,几乎和生成代码的序列化库有同样高的速度。
     5  ## hello word 
     6      package main
     7      import (
     8     	    "fmt"
     9     	    "github.com/niubaoshu/gotiny"
    10      )
    11      
    12      func main() {
    13     	    src1, src2 := "hello", []byte(" world!")
    14     	    ret1, ret2 := "", []byte{}
    15     	    gotiny.Unmarshal(gotiny.Marshal(&src1, &src2), &ret1, &ret2)
    16     	    fmt.Println(ret1 + string(ret2)) // print "hello world!"
    17      }
    18  
    19  ## 特性
    20  - 效率非常的高,是golang自带序列化库gob的3倍以上,和一般的生成代码序列化框架处于同一水平,甚至高于它们。
    21  - 除map类型外0内存申请。
    22  - 支持编码所有的除func,chan类型外的所有golang内置类型和自定义类型。
    23  - struct 类型会编码非导出字段,可通过golang tag 的方式设置不编码。
    24  - 严格的类型转换。gotiny中只有类型完全相同的才会正确编码和解码。
    25  - 编码带类型的nil值。
    26  - 可以处理循环类型,不能编码循环值,会栈溢出。
    27  - 所有可以编码的类型都会完全的解码,不论原值是什么和目标值是什么。
    28  - 编码生成的字节串不包含类型信息,生成的字节数组非常小。
    29  ## 无法处理循环值 不支持循环引用  TODO 
    30  	type a *a
    31  	var b a
    32  	b = &b
    33  
    34  ## install
    35  ```bash
    36  $ go get -u github.com/niubaoshu/gotiny
    37  ```
    38  
    39  ## 编码协议
    40  ### 布尔类型
    41  bool类型占用一位,真值编码为1,假值编码为0。当第一次遇到bool类型时会申请一个字节,将值编入最低位,第二次遇到时编入次低位,第九次遇到bool值时再申请一个字节编入最低位,以此类推。
    42  ### 整数类型
    43  - uint8和int8 类型作为一个字节编入字符串的下一个字节。
    44  - uint16,uint32,uint64,uint,uintptr 采用[Varints](https://developers.google.com/protocol-buffers/docs/encoding#varints)编码方式。
    45  - int16,int32,int64,int 采用ZigZag转换成一个无符号数后采用[Varints](https://developers.google.com/protocol-buffers/docs/encoding#varints)编码方式。
    46  
    47  ### 浮点数类型
    48  float32和float64采用[gob](https://golang.org/pkg/encoding/gob/)中对浮点类型的编码方式。
    49  ### 复数类型
    50  - complex64类型会强转为一个uint64后采用uint64的编码方式。
    51  - complex128类型分别将虚实部分作为float64类型编码。
    52  
    53  ### 字符串类型
    54  字符串类型先将字符串长度强转为uint64类型编码,然后将字符串字节数组自身原样编码。
    55  ### 指针类型
    56  指针类型判断是否为nil,如果是nil,编入一个bool类型的false值后结束,如果不为nil,编入一个bool类型true值,之后将指针解引用,按解引用后的类型编码。
    57  ### array和slice类型
    58  先将长度强转为一个uint64后采用uint64的编码方式编入,然后将每一个元素安装自身的类型编码。
    59  ### map类型
    60  同上,先编入长度,然后编入一个健,后面跟健对应的值,在编入一个健,接着是值,以此类推。
    61  ### struct类型
    62  将结构体的所有成员按其类型编码,无论是否导出,非导出的字段也会编码。结构体会严格还原。
    63  ### 实现接口的类型
    64  - 对于实现encoding包BinaryMarshaler/BinaryUnmarshaler 或 实现 gob包GobEncoder/GobDecoder 接口的类型会用实现的方法编码。
    65  - 对于实现了gotiny.GoTinySerialize包的类型将采用实现的方法编码和解码
    66  
    67  ## benchmark
    68  [benchmark](https://github.com/niubaoshu/go_serialization_benchmarks)
    69  
    70  
    71  ### License
    72  MIT
    73  
    74  [travis-img]: https://travis-ci.org/niubaoshu/gotiny.svg?branch=master
    75  [travis-url]: https://travis-ci.org/niubaoshu/gotiny
    76  [license-img]: http://img.shields.io/badge/license-MIT-green.svg?style=flat-square
    77  [license-url]: http://opensource.org/licenses/MIT
    78  [doc-img]: http://img.shields.io/badge/GoDoc-reference-blue.svg?style=flat-square
    79  [doc-url]: https://godoc.org/github.com/niubaoshu/gotiny