我正在編寫一個(gè)例程來遍歷目錄樹并為我找到的每個(gè)文件創(chuàng)建一個(gè)數(shù)字簽名(salted-hash)。在測試它時(shí),我得到了這種奇怪的行為 - 如果我給程序一個(gè)目錄“上方”的根路徑,程序可以遍歷樹并打印出文件名,但是如果我嘗試打開文件以讀取它的字節(jié),我在例程找到的文件上獲取錯(cuò)誤消息“沒有這樣的文件或目錄” - 不確定這里給出了什么。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}
GOLANG: Walk Directory Tree and Process Files
函數(shù)式編程
2021-07-07 17:50:45