我是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)}}}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句