Created
March 27, 2015 17:17
-
-
Save Partyschaum/5f2d325e621d404dc360 to your computer and use it in GitHub Desktop.
Hilfe mit Rekursion und Structs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strconv" | |
) | |
type Entry struct { | |
id int | |
title string | |
parent *Entry | |
children EntryList | |
} | |
type EntryList []Entry | |
func (entryList *EntryList) addEntry(id, parent_id int, title string) (newEntry *Entry, err error) { | |
if parent_id == 0 { | |
fmt.Println("add entry in level 0", entryList, id, parent_id, title) | |
newEntry = &Entry{id, title + "-" + strconv.Itoa(id), nil, EntryList{}} | |
*entryList = append(*entryList, *newEntry) | |
fmt.Println(entryList) | |
} else { | |
for _, entry := range *entryList { | |
if entry.id == parent_id { | |
fmt.Println(entry, id, parent_id) | |
newEntry, err = entry.children.addEntry(id, 0, title) | |
fmt.Println(newEntry) | |
newEntry.parent = &entry | |
} | |
} | |
} | |
return | |
} | |
func main() { | |
var list EntryList | |
// create list of entries | |
for i := 1; i <= 10; i++ { | |
list.addEntry(i, 0, "entry") | |
} | |
// create some children | |
for i := 1; i <= 10; i++ { | |
list.addEntry(i, 1, "child") | |
} | |
for _, entry := range list { | |
fmt.Println(entry) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment