我有一個非常具體的問題,我找不出解決方案。轉換地圖到樹中去
我有一個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}' –
哎呀!試圖在平板電腦上輸入代碼不利於獲得好的結果!我無法相信我做了一件愚蠢的事!我現在就解決它。謝謝你指出! –