Hi I need to do some bidirectional lockup and need some caind of map structure like map[key][key] are there some think like that in Go? Or what is the best way to go about doing it?
1条回答 默认 最新
- dongzong8110 2013-02-18 23:14关注
There's no such thing in the language or the library (AFAIK), but they're easy enough to implement: just combine two maps in a
struct
and make sure they stay in sync. The only problem is that it's hard to write these in a generic manner, but that can be done usinginterface{}
:type BidirMap struct { left, right map[interface{}]interface{} } func (m *BidirMap) Insert(key, val interface{}) { if _, inleft := left[key]; inleft { delete(left, key) } if _, inright := right[val]; inright { delete(right, val) } m.left[key] = val m.right[val] = key }
etc.
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报