#gotiny
Gotiny is an efficiency-oriented go language serialization library. Gotiny improves efficiency by pre-generating the encoder and reducing the use of the reflect library, almost as fast as the serialization library that generates the code.
package main
import (
"fmt"
"github.com/niubaoshu/gotiny"
)
func main() {
src1, src2 := "hello", []byte(" world!")
ret1, ret2 := "", []byte{}
coder := gotiny.New(src1, src2)
coder.Decode(coder.Encode(&src1, &src2), &ret1, &ret2)
fmt.Println(ret1 + string(ret2)) // print "hello world!"
}- The efficiency is very high, which is more than 3 times that of golang's own serialization library gob, which is at the same level and even higher than the general generated code serialization framework.
- 0 memory application except map type.
- Supports encoding all golang built-in types and custom types except for func, chan types.
- The struct type encodes non-exported fields, which can be set without the encoding by way of golang tag.
- Strict type conversion. Only the same type of gotiny will be correctly encoded and decoded.
- Encode the nil value of the band type.
- Can handle loop types, can't encode loop values, stack overflow.
- All types that can be encoded are completely decoded, regardless of the original value and what the target value is.
- The byte string generated by the encoding does not contain type information, and the generated byte array is very small. – Threadsafe, once created scheme can be used from different goroutines
Type a *a
Var b a
b = &b$ go get -u github.com/niubaoshu/gotinyThe bool type occupies one bit, the true value is encoded as 1, and the false value is encoded as 0. When the bool type is encountered for the first time, it will apply for one byte, the value will be programmed into the lowest position, the second time will be programmed into the next low position, and the ninth time when the bool value is encountered, then apply for one byte to be programmed into the lowest position. And so on.
- uint8 and int8 types are encoded as one byte into the next byte of the string.
- uint16, uint32, uint64, uint, uintptr use [Varints] (https://developers.google.com/protocol-buffers/docs/encoding#varints) encoding.
- int16, int32, int64, int Use ZigZag to convert to an unsigned number and then use [Varints] (https://developers.google.com/protocol-buffers/docs/encoding#varints) encoding.
Float32 and float64 use the encoding of floating point types in gob.
- The complex64 type will be strongly converted to a uint64 and then encoded using uint64.
- The complex128 type encodes the virtual real part as a float64 type.
The string type first converts the length of the string to a uint64 [Varints] type encoding, and then encodes the string byte array itself.
The pointer type determines whether it is nil. If it is nil, it ends with a false value of bool type. If it is not nil, it encodes a bool type true value, then dereferences the pointer and encodes the type after dereferencing.
First convert the length to a uint64 and then encode it using uint64 encoding, then install each element's own type encoding.
Same as above, first compile the length, then compile a health, followed by the value corresponding to the key, compile a key, then a value, and so on.
All members of a structure are encoded by their type, and non-exported fields are encoded regardless of whether they are exported or not. The structure will be strictly restored.
- The type that implements the encoding package BinaryMarshaler/BinaryUnmarshaler or implements the gob package GobEncoder/GobDecoder interface is encoded with the implementation method.
- For implementations of the type of gotiny.GoTinySerialize package will be encoded and decoded using the implemented method
Schemes allow perform data migrations, deserialise data into objects which has slightly different fields than ones, which was used for data serialisation.
type T1 struct {
I uint32
Str string
}
type T2 struct { // different order binary format is different
Str string
I uint32
}
func main() {
coder1 = gotiny.New(T1{})
coder2 = gotiny.New(T2{})
schemeJSON := coder1.GetScheme().AsJSON()
scheme1, _ := gotiny.SchemeFromJSON(schemeJSON)
coder2.SetScheme(scheme1)
// Encoding part
encoded := coder1.Encode(T1{32, "wow"})
result := T2{}
coder2.Decode(encoded, &result)
}MIT