douao1858 2015-08-12 09:20
浏览 85
已采纳

golang无法解析json以创建反射对象

I try to write simple message protocol in go and i've encountered a problem. I have a lot of message types and i want to have a dictionary like this to manipulate with messages:

var dict map[reflect.Type]int = map[reflect.Type]int{
    reflect.TypeOf(DataMessage{}):          1000,
    reflect.TypeOf(TextMessage{}):          1001,
    //....
}

func GetMessageTypeId(value interface{}) int {
    if id, ok := dict[reflect.TypeOf(value)]; ok {
        return id
    } else {
        return -1
    }
}

func GetValueByTypeId(typeId int) interface{} {
    for typeDec, id := range dict {
        if id == typeId {
            return reflect.Zero(typeDec).Interface()
        }
    }
    fmt.Println("Unknown message type", typeId)
    return nil
}

It works fine, but when i instantiate message with GetValueByTypeId and try to unmarshall json into it - i receive map[string]interface instead of my message. I've made simple example to reproduce the problem:

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

  • 写回答

2条回答 默认 最新

  • dqgxazo4483 2015-08-12 10:13
    关注

    Please read this article: http://research.swtch.com/interfaces, especially the "Memory Optimizations".

    The interface{} by definition consists of two pointers - to method table (e.g. type) and to data it holds. So for

    var destination3 interface{} = reflect.Zero(reflect.TypeOf(Message{})).Interface()
    

    it is empty method table (as interface{} has no methods) and reference to Message{}. Taking reference from it returns the reference to this struct so the unmarhal overwrites it with whatever matches interface{}.

    If the data interface{} variable holds is a pointer itself, then it is optimized in a way that this pointer is used instead creating interface{} structure. So getting reference to it gives the reference to original variable.

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

    package main
    
    import "fmt"
    
    func main() {
        var m1 struct{ Data string }
        var m2 interface{}
        var m3 interface{}
    
        m2 = &m1
        m3 = m1
    
        fmt.Printf("&m1=%p m2=%p &m3=%p
    ", &m1, m2, &m3)
    }
    

    In your case, using Zero is equivalent to m3 in the example above. Using New is equivalent to m2.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图