Reputation: 5793
I am new to golang and trying to explore the lang with sample hobby project for that I need to write the below tree like structure. Its like File system, one Folder will have many Folder and files. And tree structure goes on until it has no further branch.
[Fol]
[Fol,Fol,Fol] [Fil,Fil,Fil]
My solution to have:
type Fol struct{
slice of Fol
slice of Fil
}
Its taking time for me to design, So any once help is very much appreciated.
Regards, Vineeth
Finally I used solution provided in below link: https://stackoverflow.com/a/12659537/430294
Upvotes: 1
Views: 3853
Reputation: 54079
Something like this?
package main
import "fmt"
type File struct {
Name string
}
type Folder struct {
Name string
Files []File
Folders []Folder
}
func main() {
root := Folder{
Name: "Root",
Files: []File{
{"One"},
{"Two"},
},
Folders: []Folder{
{
Name: "Empty",
},
},
}
fmt.Printf("Root %#v\n", root)
}
Prints
Root main.Folder{Name:"Root", Files:[]main.File{main.File{Name:"One"}, main.File{Name:"Two"}}, Folders:[]main.Folder{main.Folder{Name:"Empty", Files:[]main.File(nil), Folders:[]main.Folder(nil)}}}
Upvotes: 5