英文:
golang fails to parse json for reflection created object
问题
我尝试在Go语言中编写一个简单的消息协议,但遇到了一个问题。我有很多消息类型,我想要一个类似于这样的字典来处理消息:
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
}
它工作得很好,但是当我使用GetValueByTypeId实例化消息并尝试将JSON解组成它时,我收到的是map[string]interface而不是我的消息。
我创建了一个简单的示例来重现这个问题:
http://play.golang.org/p/QEyDN9vztr
英文:
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:
答案1
得分: 2
请阅读这篇文章:http://research.swtch.com/interfaces,特别是"Memory Optimizations"部分。
根据定义,interface{}由两个指针组成——一个指向方法表(例如类型),一个指向它所持有的数据。因此,在以下代码中:
var destination3 interface{} = reflect.Zero(reflect.TypeOf(Message{})).Interface()
它是一个空的方法表(因为interface{}
没有方法),并且引用了Message{}
。从中获取引用会返回对该结构体的引用,因此unmarshal会用与interface{}
匹配的内容覆盖它。
如果interface{}
变量所持有的数据本身是一个指针,那么它会被优化,使用该指针而不是创建interface{}
结构体。因此,获取对它的引用会返回对原始变量的引用。
在你的情况下,使用Zero等同于上面示例中的m3。使用New等同于m2。
英文:
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.
答案2
得分: 0
我找到了如何做我需要的事情的方法:
val := reflect.New(reflect.TypeOf(Message{}))
json.Unmarshal(data, val.Interface())
return val.Elem().Interface()
http://play.golang.org/p/8g9FSg3MSj
但是第一个版本有什么问题吗?看起来reflect.Zero(type)
应该等同于reflect.New(type).Elem()
,我错了吗?
英文:
I've found the way how to do what i need
val := reflect.New(reflect.TypeOf(Message{}))
json.Unmarshal(data, val.Interface())
return val.Elem().Interface()
http://play.golang.org/p/8g9FSg3MSj
But was was wrong wit the first version???
It Looks like reflect.Zero(type)
should be equivalent to reflect.New(type).Elem()
- am i wrong?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论