我有一个非常具体的问题,我找不出解决方案。转换地图到树中去
我有一个map[string]Metric
,我想转换成一个树在前端使用。 Metric
界面看起来有一个Path()
和Name()
方法,name方法返回句点分隔的路径的最后一部分(所以'my.awesome.metric'的路径将表示这个度量的名称为'metric') 树应按路径排序,并应包含IndexNode
s。这个结构是这样的:
type IndexNode struct {
Name string
Path string
Children []*IndexNode
}
所以地图是这样的:
{
my.awesome.metric.downloads
my.awesome.othermetric.downloads
my.awesome.othermetric.uploads
my.other.cool.metric
}
应该导致这样的树:(抱歉粗ASCII艺术)
+-- other -- cool -- metric
|
my --+ +-- metric -- downloads
| |
+-- awesome --+ +-- downloads
| |
+-- othermetric --+
|
+-- uploads
注我只有一个根节点(我在这种情况下)。树内的顺序对我无关紧要。
我尽力而为,想不出来......经过大量的googleing(只显示我如何创建二叉搜索树和GoDS库),我辞职并决定在这里问我第一个问题
感谢您的帮助!
这已经是一个很好的起点,但是使用以下代码:'root:= NewTree(nil,nil,false); NewTree ,[] string {“jooy”,“bluwhale”,“files”},true); NewTree(root,[] string {“jooy”,“bluwhale”,“users”},true); NewTree(root, ] string {“jooy”,“dexter”,“registrations”},true)',结果如下(错误)JSON:'{“children”:{“jooy”:{“children”:{“dexter”: {“children”:{“registrations”:{“children”:{},“data”:true}},“data”:false}},“data”:false}},“data”:false}' –
哎呀!试图在平板电脑上输入代码不利于获得好的结果!我无法相信我做了一件愚蠢的事!我现在就解决它。谢谢你指出! –