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

编程入门 行业动态 更新时间:2024-10-23 01:52:12
本文介绍了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]接口而不是我的消息。 我做了一个简单的例子来重现问题:

play.golang/p/QEyDN9vztr

解决方案

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

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

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

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

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

play.golang/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:

play.golang/p/QEyDN9vztr

解决方案

Please read this article: research.swtch/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.

play.golang/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以创建反射对象

本文发布于:2023-07-30 00:36:56,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   对象   golang   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!