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:
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
it is empty method table (as
interface{}
has no methods) and reference toMessage{}
. Taking reference from it returns the reference to this struct so the unmarhal overwrites it with whatever matchesinterface{}
.If the data
interface{}
variable holds is a pointer itself, then it is optimized in a way that this pointer is used instead creatinginterface{}
structure. So getting reference to it gives the reference to original variable.http://play.golang.org/p/KsIS29rUAX
In your case, using Zero is equivalent to m3 in the example above. Using New is equivalent to m2.
I've found the way how to do what i need
http://play.golang.org/p/8g9FSg3MSj
But was was wrong wit the first version??? It Looks like
reflect.Zero(type)
should be equivalent toreflect.New(type).Elem()
- am i wrong?