Golang and Ztree

Source: Internet
Author: User
Tags lstat
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} 
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.