前言

半个月感觉没收获多少,主要学习了加密解密、编译原理、Windows内核相关基础教程、Golang的学习,并没有学完,一直在坚持和学校一样的教学体系,每天的课程是不一样的,不会导致学习疲劳,三分热度,一直抱着一本书看下去,几天就失了兴趣,比如编译原理…

一步一jo印

中序

之前学习Java时,没有想到写一个遍历指定目录的程序,最近在学习Golang,想着用GO语言实现以下。

思路

  1. 指定需要遍历的目录
  2. 列出目录下的所有文件以及文件夹
  3. 打印文件名或文件夹名
  4. 判断是否为文件夹,是则去第1步,否则去第5步
  5. 退出

后续

用递归的方式实现该程序

package main

import (
    "fmt"
    "io/ioutil"
)

func main() {
    listDir("/Users/bufsnake", 0)
}

func listDir(dir string, level int) {
    s := ""
    for i := 0; i < level; i++ {
        s = s + "   "
    }
    fileInfos, err := ioutil.ReadDir(dir)
    if err != nil {
        fmt.Println(err)
    }
    for _, file := range fileInfos {
        fi := dir + "/" + file.Name()
        fmt.Println(s + fi)
        if file.IsDir() {
            listDir(fi, level+1)
        }
    }
}

运行结果

☁  golang  go run mulubianli.go
/Users/bufsnake/golang/_
   /Users/bufsnake/golang/_/main.go
/Users/bufsnake/golang/array.go
/Users/bufsnake/golang/bufio
   /Users/bufsnake/golang/bufio/main.go
/Users/bufsnake/golang/ch1
   /Users/bufsnake/golang/ch1/dup1
      /Users/bufsnake/golang/ch1/dup1/main.go
   /Users/bufsnake/golang/ch1/dup2
      /Users/bufsnake/golang/ch1/dup2/file
      /Users/bufsnake/golang/ch1/dup2/main.go
   /Users/bufsnake/golang/ch1/echo1
      /Users/bufsnake/golang/ch1/echo1/main.go
   /Users/bufsnake/golang/ch1/echo2
      /Users/bufsnake/golang/ch1/echo2/main.go
   /Users/bufsnake/golang/ch1/echo3
      /Users/bufsnake/golang/ch1/echo3/main.go
   /Users/bufsnake/golang/ch1/helloworld
      /Users/bufsnake/golang/ch1/helloworld/README.md
      /Users/bufsnake/golang/ch1/helloworld/main.go
   /Users/bufsnake/golang/ch1/server1
      /Users/bufsnake/golang/ch1/server1/mian.go
/Users/bufsnake/golang/chabie
   /Users/bufsnake/golang/chabie/chabie
   /Users/bufsnake/golang/chabie/main.go
/Users/bufsnake/golang/deferDemo.go
/Users/bufsnake/golang/deferDemo1.go
/Users/bufsnake/golang/exception.go
/Users/bufsnake/golang/func.go
/Users/bufsnake/golang/gob_msgpack.go
/Users/bufsnake/golang/main.go
/Users/bufsnake/golang/mapDemo.go
/Users/bufsnake/golang/mulubianli.go
/Users/bufsnake/golang/rand
   /Users/bufsnake/golang/rand/main.go
/Users/bufsnake/golang/strconv
   /Users/bufsnake/golang/strconv/main.go
/Users/bufsnake/golang/time
   /Users/bufsnake/golang/time/main.go
/Users/bufsnake/golang/web
   /Users/bufsnake/golang/web/ch1
      /Users/bufsnake/golang/web/ch1/login.gtpl
      /Users/bufsnake/golang/web/ch1/main
      /Users/bufsnake/golang/web/ch1/main.go
   /Users/bufsnake/golang/web/ginDemo.go

总结

Go Go Golang



Go      golang go

本博客所有文章除特别声明外,均采用 CC BY-SA 3.0协议 。转载请注明出处!