小编典典

gob:类型未注册的接口:map [string] interface {}

go

gob 无法编码 map[string]interface{}

gob: type not registered for interface: map[string]interface {}

http://play.golang.org/p/Si4hd8I0JE

package main

import (
    "bytes"
    "encoding/gob"
    "encoding/json"
    "fmt"
    "log"
)

func CloneObject(a, b interface{}) []byte {
    buff := new(bytes.Buffer)
    enc := gob.NewEncoder(buff)
    dec := gob.NewDecoder(buff)
    err := enc.Encode(a)
    if err != nil {
        log.Panic("e1: ", err)
    }
    b1 := buff.Bytes()
    err = dec.Decode(b)
    if err != nil {
        log.Panic("e2: ", err)
    }
    return b1
}

func main() {
    var a interface{}
    a = map[string]interface{}{"X": 1}
    b2, err := json.Marshal(&a)
    fmt.Println(string(b2), err)

    var b interface{}
    b1 := CloneObject(&a, &b)
    fmt.Println(string(b1))
}

可以map[string]interface{}在gob中编码吗?我可以用JSON编码


阅读 281

收藏
2020-07-02

共1个答案

小编典典

gob.Register(map[string]interface{}{})

http://play.golang.org/p/Dd3IzJgl0A

2020-07-02