【原创】go语言之打印目录

时间:2024-11-29 21:35:19
package main

import (
"fmt"
"io/ioutil"
"log"
) func listFiles(dirname string, level int) {
// level用来记录当前递归的层次
// 生成有层次感的空格
s := "|--"
for i := 0; i < level; i++ {
s = "| " + s
} fileInfos, err := ioutil.ReadDir(dirname)
if err != nil{
log.Fatal(err)
}
for _, fi := range fileInfos {
if fi.Name() == ".git"{
continue
}
filename := dirname + "/" + fi.Name()
fmt.Printf("%s%s\n", s, fi.Name())
if fi.IsDir() {
//继续遍历fi这个目录
listFiles(filename, level+1)
}
}
}
func main() {
dirname := "C:\\Users\\Administrator\\Desktop\\七星\\配置文件\\qxonlinebak"
listFiles(dirname,0)
}