| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package gob
- import (
- "bytes"
- "encoding/gob"
- "fmt"
- "testing"
- )
- type msgData struct {
- X, Y, Z int
- Name string
- Value any
- }
- type msgDataValue struct {
- Name string
- }
- func TestGOB(t *testing.T) {
- gob.Register(msgDataValue{})
- var buf bytes.Buffer
- enc := gob.NewEncoder(&buf)
- //
- sendMsg := msgData{
- X: 3,
- Y: 4,
- Z: 5,
- Name: "name",
- Value: msgDataValue{Name: "DataValue"},
- }
- fmt.Println("原始数据:", sendMsg)
- err := enc.Encode(&sendMsg)
- fmt.Println(err)
- fmt.Println("传递的编码数据为:", buf)
- dec := gob.NewDecoder(&buf)
- sendMsgDecode := msgData{}
- err = dec.Decode(&sendMsgDecode)
- fmt.Println(err)
- fmt.Println(sendMsgDecode)
- }
|