This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
packageutil
import(
"io"
"os"
"path/filepath"
)
// Exists reports whether the named file or directory exists.
funcExists(namestring)bool{
if_,err:=os.Stat(name);err!=nil{
ifos.IsNotExist(err){
returnfalse
}
}
returntrue
}
// CreatNestedFile 给定path创建文件,如果目录不存在就递归创建
funcCreatNestedFile(pathstring)(*os.File,error){
basePath:=filepath.Dir(path)
if!Exists(basePath){
err:=os.MkdirAll(basePath,0700)
iferr!=nil{
Log().Warning("无法创建目录,%s",err)
returnnil,err
}
}
returnos.Create(path)
}
// IsEmpty 返回给定目录是否为空目录
funcIsEmpty(namestring)(bool,error){
f,err:=os.Open(name)
iferr!=nil{
returnfalse,err
}
deferf.Close()
_,err=f.Readdirnames(1)// Or f.Readdir(1)
iferr==io.EOF{
returntrue,nil
}
returnfalse,err// Either not empty or error, suits both cases