dongwei4444
2018-02-05 17:09
浏览 34
已采纳

在go结构中初始化深层地图嵌套

To initialize a map in a struct one should do the following:

someStruct.nestedMap = make(map[int8]int8)

But what should you do if you have a code structure like this:

type Base struct {
    base map[int8]uint64
}

type Middle struct {
    baseObjects map[int8]Base
}

type Top struct {
    middleObjects map[int8]Middle
}

There we have a total of 3 structs where each has a struct as a key. How do you initialize this, and make it ready to go?

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

要在结构中初始化映射,应执行以下操作:

   someStruct.nestedMap = make(map [int8] int8)
   
 
 

但是,如果您具有这样的代码结构,该怎么办:

  type基础结构{
基础结构图[int8] uint64 
} 
 
type中间结构{
 baseObjects map [int8] Base 
} 
 
type顶部结构{  
 middleObjects map [int8] Middle 
} 
   
 
 

共有3个结构,每个结构都有一个键作为结构体。 如何初始化 并准备好了吗?

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

2条回答 默认 最新

  • donglong9745 2018-02-06 17:55
    最佳回答

    What you want is auctually a map with default value that is not zero value, but a value ready to use, in this case, a made map.

    Before going to the solution, there auctually is an issue about go here: https://github.com/golang/go/issues/3117

    For reasons, Go now does not support to assign to a field of a struct if the struct is stored in a map.

    To solve the issue, you need to use a pointer to that struct, and store the pointer in the map instead. So your data structure needs to be changed like:

    type Base struct {
        base map[int8]uint64
    }
    
    type Middle struct {
        baseObjects map[int8]*Base
    }
    
    type Top struct {
        middleObjects map[int8]*Middle
    }
    

    And to the core logic: a defualt value map. Go does not support that with plain maps, but we can extend it, using methods to wrap it around.

    func (t Top) Get(i int8) *Middle {
        x, ok := t.middleObjects[i]
        if !ok {
            v := NewMiddle()
            t.middleObjects[i] = v
            return v
        }
    
        return x
    }
    
    func (m Middle) Get(i int8) *Base {
        x, ok := m.baseObjects[i]
        if !ok {
            v := NewBase()
            m.baseObjects[i] = v
            return v
        }
    
        return x
    }
    

    When we are trying to get a value, we first check if it exists. If not, we return a new constructed one, and if it did exist, return the value.

    Usage:

    t.Get(8).Get(9).base[10] = 14
    

    Playground example: https://play.golang.org/p/0JSN0yjRPif

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题