golang MessagePack 序列化

MessagePack 是一个基于二进制高效的对象序列化类库

go  get  github.com/vmihailenco/msgpack/v5
package main

import (
	"fmt"

	"github.com/vmihailenco/msgpack/v5"
)

type Strcut1 struct {
	Name  string
	Age   int
	Hight int
}

func main() {
	sperson := []Strcut1{ //person结构类型的切片
		{Name: "aa", Age: 3, Hight: 54},
		{"aa", 2, 54},
		{"aa", 6, 54},
		{"aa", 3, 52},
		{"aa", 12, 15},
	}

	//序列化
	a, err := msgpack.Marshal(sperson)
	if err != nil {
		fmt.Printf("err: %v\n", err)
	}
	fmt.Printf("a: %T %[1]v\n", a) //a: []uint8 [149 131 164 78 97 109 101 162 97 97 163 65 103 101 3 165 72 105 103 104 116 54 131 164 78 97 109 101 162 97 97 163 65 103 101 2 165 72 105 103 104 116 54 131 164 78 97 109 101 162 97 97 163 65 103 101 6 165 72 105 103 104 116 54 131 164 78 97 109 101 162 97 97 163 65 103 101 3 165 72 105 103 104 116 52 131 164 78 97 109 101 162 97 97 163 65 103 101 12 165 72 105 103 104 116 15]

	//反序列化
	var as []Strcut1
	err = msgpack.Unmarshal(a, &as)
	fmt.Printf("as: %T %[1]v\n", as)//as: []main.Strcut1 [{aa 3 54} {aa 2 54} {aa 6 54} {aa 3 52} {aa 12 15}]

}

你可能感兴趣的:(GO,golang,开发语言,后端)