With the information provided in your question and comment, what could solve your problem is an ordinary recursive loop.
package main
import "fmt"
type Org struct {
OrgID string
OrgName string
parentID string
}
func printTree(tbl []Org, parent string, depth int) {
for _, r := range tbl {
if r.parentID == parent {
for i := 0; i < depth; i++ {
fmt.Print("--")
}
fmt.Print(r.OrgName, "\n\n")
printTree(tbl, r.OrgID, depth+1)
}
}
}
func main() {
data := []Org{
{"A001", "Dept", "0 -----th top"},
{"A002", "subDept1", "A001"},
{"A003", "sub_subDept", "A002"},
{"A006", "gran_subDept", "A003"},
{"A004", "subDept2", "A001"},
}
printTree(data, "0 -----th top", 0)
}
Result:
Dept
--subDept1
----sub_subDept
------gran_subDept
--subDept2
Playground: http://play.golang.org/p/27CQAhI8gf
Be aware that this recursive function might get stuck in an loop incase there if a parent is the descendant of its own child.