我正在一个文件夹中创建一个目录
// 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
}
上述代码返回此错误:
错误(syscall.Errno)EACCES(13)
我可以再次检查目录是否已实际创建。但是权限是有限的
d---------
> ls -lh
d--------- 2 m3 users 6 Dec 23 20:30 test-files
如何创建具有相应权限的目录?