2015-08-12 09:20
浏览 84


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:

图片转代码服务由CSDN问答提供 功能建议

我尝试在go中编写简单的消息协议,但遇到了问题。 我有很多消息类型,我想有一个像这样的字典来处理消息:

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

func GetMessageTypeId(值接口{}  )int {
如果是id,可以:= dict [reflect.TypeOf(value)]; 好的{
func GetValueByTypeId(typeId int)接口{} {
如果是id  == typeId {

它工作正常,但是当我使用GetValueByTypeId实例化消息并尝试将json编组到其中时-我收到map [string] interface而不是我的消息。 我已经做了一个简单的例子 重现该问题: p / QEyDN9vztr

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dqgxazo4483 2015-08-12 10:13

    Please read this article:, 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.

    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.

    解决 无用
    打赏 举报

相关推荐 更多相似问题