我正在像這樣的單元測驗中創建一個目錄:
// The directory which is going to be created next to unit test executable.
const DirName string = "test-files"
創建目錄:
// Create the directory if doesn't already exist.
err := os.MkdirAll(DirName, os.ModeDir)
if err != nil {
return err
}
// Compose a sample file path to double-check its existence.
pth := DirName string(os.PathSeparator) "samplefile.txt"
exists, err := doesExist(pth)
if err != nil {
return err
}
并檢查檔案是否存在:
// Does file exist?
func doesExist(pth string) (bool, error) {
// Check file existence
// https://stackoverflow.com/a/10510718/3405291
if _, err := os.Stat(pth); err != nil {
if os.IsNotExist(err) {
return false, nil
} else {
return true, err
}
}
// Above conditions are skipped,
// therefore file exists.
return true, nil
}
上面的代碼回傳這個錯誤:
os.Stat:權限被拒絕
錯誤(系統呼叫。Errno)EACCES(13)
我可以仔細檢查該目錄是否已實際創建。但權限是d---------:
> ls -lh
d--------- 2 m3 users 6 Dec 23 20:30 test-files
如何創建具有適當權限的目錄?
uj5u.com熱心網友回復:
你在濫用os.ModeDir常量。它被發明來作為位掩碼來檢查檔案的模式 權限位是否(例如,回傳,os.(*File).Stat表明它是一個目錄。
創建目錄的默認權限模式位是,0777但它們受umask.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/392439.html
標籤:去
上一篇:golang是快取DNS嗎?
