mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-08-15 02:51:34 +00:00
fmr code
This commit is contained in:
@@ -32,7 +32,6 @@ ioutil.ReadDir函數會返迴一個os.FileInfo類型的slice,os.FileInfo類型
|
||||
|
||||
下面的主函數,用了兩個goroutine。後台的goroutine調用walkDir來遍歷命令行給出的每一個路徑併最終關閉fileSizes這個channel。主goroutine會對其從channel中接收到的文件大小進行纍加,併輸出其和。
|
||||
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -75,7 +74,9 @@ func printDiskUsage(nfiles, nbytes int64) {
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
這個程序會在打印其結果之前卡住很長時間。
|
||||
|
||||
```
|
||||
$ go build gopl.io/ch8/du1
|
||||
$ ./du1 $HOME /usr /bin /etc
|
||||
@@ -176,11 +177,8 @@ func dirents(dir string) []os.FileInfo {
|
||||
sema <- struct{}{} // acquire token
|
||||
defer func() { <-sema }() // release token
|
||||
// ...
|
||||
|
||||
```
|
||||
|
||||
這個版本比之前那個快了好幾倍,盡管其具體效率還是和你的運行環境,機器配置相關。
|
||||
|
||||
練習8.9: 編寫一個du工具,每隔一段時間將root目録下的目録大小計算併顯示出來。
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user