亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

GOLANG: Walk Directory Tree and Process Files

GOLANG: Walk Directory Tree and Process Files

Go
函數式編程 2021-07-07 17:50:45
我正在編寫一個例程來遍歷目錄樹并為我找到的每個文件創建一個數字簽名(salted-hash)。在測試它時,我得到了這種奇怪的行為 - 如果我給程序一個目錄“上方”的根路徑,程序可以遍歷樹并打印出文件名,但是如果我嘗試打開文件以讀取它的字節,我在例程找到的文件上獲取錯誤消息“沒有這樣的文件或目錄” - 不確定這里給出了什么。Walk() 例程如何“看到”文件,但 ioutil.ReadFile() 無法找到它?示例代碼:// start with path higher up the tree, say $HOMEfunc doHashWalk(dirPath string) {    err := filepath.Walk(dirPath, walkFn)    // Check err here}func walkFn(path string, fi os.FileInfo, err error) (e error) {    if !fi.IsDir() {        // if the first character is a ".", then skip it as it's a hidden file        if strings.HasPrefix(fi.Name(), ".") {            return nil        }        // read in the file bytes -> get the absolute path        fullPath, err := filepath.Abs(fi.Name())        if err != nil {            log.Printf("Abs Err: %s", err)        }        // THIS ALWAYS FAILED WITH ERROR        bytes, err := ioutil.ReadFile(fullPath) // <-- (fi.Name() also doesn't work)        if err != nil {            log.Printf("Err: %s, Bytes: %d", err, len(bytes))             }        // create the salted hash        ...    }    return nil}
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 332 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號