golang无法解析json以创建反射对象 [英] golang fails to parse json for reflection created object

查看:317
本文介绍了golang无法解析json以创建反射对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写简单的消息协议,并且遇到了问题。我有很多消息类型,我想要这样的字典来处理消息:

  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(未知消息类型,typeId)
返回零
}

它工作正常,但是当我使用GetValueByTypeId实例化消息并尝试解组json时 - 我收到map [string]接口而不是我的消息。
我做了一个简单的例子来重现问题:

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

解决方案

请阅读文章: http://research.swtch.com/interfaces ,特别是内存优化。 p>

定义中的接口{}由两个指针组成 - 包括方法表(例如类型)和它所保存的数据。因此,对于

  var destination3接口{} = reflect.Zero(reflect.TypeOf(Message {}))。 b $ b  

它是空的方法表(如 interface {} 没有方法)并引用 Message {} 。通过引用它可以返回对此结构的引用,所以unmarhal用任何匹配 interface {} 覆盖它。



如果数据 interface {} 变量持有本身就是一个指针,那么它会以使用该指针的方式进行优化,而不是创建 interface {} 结构。因此,引用它可以引用原始变量。



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

 包主

importfmt

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 \ n,& m1,m2,&m3)
}

,在上面的例子中使用零等于m3。使用New等于m2。


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

解决方案

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\n", &m1, m2, &m3)
}

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

这篇关于golang无法解析json以创建反射对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆