This is a creation in Article, where the information may have evolved or changed.
package mainimport ("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) {//List all directories in the current directory, file files: = listfiles (path)//Traverse These files for _, FileName: = Range Files {//stitching full path Fpat H: = FilePath. Join (path, filename)//construct file Structure Fio, _: = OS. Lstat (Fpath)//Add the current file as a child node to the directory: = Filenode{filename, Fpath, []*filenode{}}node. Filenodes = Append (node. Filenodes, &child)//If the current file being traversed is a directory, enter the directory for recursive if Fio. Isdir () {Walk (Fpath, Fio, &child)}}return}func listfiles (dirname string) []string {F, _: = OS. Open (dirname) names, _: = F.readdirnames ( -1) f.close () sort. Strings (names) return names}