繁体   English   中英

golang树状文件系统解决方案

[英]golang Tree like solution for Filesystem

我是golang的新手,并尝试通过示例爱好项目探索lang,因为我需要编写以下树状结构。 就像文件系统一样,一个文件夹将包含许多文件夹和文件。 树结构一直继续到没有其他分支为止。

          [Fol]

 [Fol,Fol,Fol] [Fil,Fil,Fil]

我的解决方案有:

type Fol struct{
    slice of Fol
    slice of Fil
}

我需要花时间设计,因此非常感谢您曾经提供的帮助。

问候,Vineeth

最后,我使用了以下链接中提供的解决方案: https : //stackoverflow.com/a/12659537/430294

像这样吗

游乐场链接

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM