Original post

I try to figure out how to make a tree from a flat structure.
I have this:
package main
import “fmt”

//DataFlat node
type DataFlat struct {
IDGeoRegion int
IDParent int
Description string
}

func main() {

dFlatlist := []DataFlat{}

dFlat := DataFlat{IDGeoRegion: 1, IDParent: 0, Description: "Organisation"}
dFlatlist = append(dFlatlist, dFlat)

dFlat = DataFlat{IDGeoRegion: 2, IDParent: 1, Description: "Office"}
dFlatlist = append(dFlatlist, dFlat)

dFlat = DataFlat{IDGeoRegion: 3, IDParent: 2, Description: "Office Zürich"}
dFlatlist = append(dFlatlist, dFlat)

dFlat = DataFlat{IDGeoRegion: 4, IDParent: 2, Description: "Office Brazil"}
dFlatlist = append(dFlatlist, dFlat)

dFlat = DataFlat{IDGeoRegion: 5, IDParent: 1, Description: "Storage"}
dFlatlist = append(dFlatlist, dFlat)

dFlat = DataFlat{IDGeoRegion: 6, IDParent: 5, Description: "Storge Zürich"}
dFlatlist = append(dFlatlist, dFlat)

dFlat = DataFlat{IDGeoRegion: 7, IDParent: 5, Description: "Storge Brazil"}
dFlatlist = append(dFlatlist, dFlat)

fmt.Println(dFlatlist)
//Result: [{1 0 Organisation} {2 1 Office} {3 2 Office Zürich} {4 2 Office Brazil} {5 1 Storage} {6 5 Storge Zürich} {7 5 Storge Brazil}]

}

Now i need a recursive function that processes dFlatList and builds it as a tree structure with this format:

type GeoTree struct {
IDGeoRegion int
IDParent int
Description string
Children []*GeoTree
}

Any help or advise would be greatly appreciated

Thanks
Theo