package main
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"sort"
)
func main() {
rootpath := "D:\\projects"
root := FileNode{"projects", rootpath, []*FileNode{}}
fileInfo, _ := os.Lstat(rootpath)
walk(rootpath, fileInfo, &root)
data, _ := json.Marshal(root)
fmt.Printf("%s", data)
}
type FileNode struct {
Name string `json:"name"`
Path string `json:"path"`
FileNodes []*FileNode `json:"children"`
}
func walk(path string, info os.FileInfo, node *FileNode) {
// 列出当前目录下的所有目录、文件
files := listFiles(path)
// 遍历这些文件
for _, filename := range files {
// 拼接全