1

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

4

1 回答 1

5

像这样的东西?

游乐场链接

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)
}

印刷

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)}}}
于 2013-09-29T16:30:33.160 回答