操作目录
基本目录操作
涉及:创建目录、重命名目录、删除目录
package main
import (
"fmt"
"os"
"time"
"path/filepath"
)
func CreateDir(Path string) string {
// 创建以当天日期命名的目录
dirName := time.Now().Format("20060102")
dirPath := filepath.Join(Path, dirName)
if _,err := os.Stat(dirPath); os.IsNotExist(err) {
// 创建目录
os.MkdirAll(dirPath, os.ModePerm)
// 修改权限
os.Chmod(dirPath, 0777)
}
return dirPath
}
func main() {
path,err := os.Getwd()
if err != nil {
fmt.Println(err)
}
path = CreateDir(path)
fmt.Println(path)
// 创建子目录。20211219/新文件夹/dic
os.MkdirAll(filepath.Join(path, "新文件夹/dic"), os.ModePerm)
// 目录重命名。20211219/newfolder/dic
os.Rename(filepath.Join(path, "新文件夹"), filepath.Join(path, "newfolder"))
// 删除目录。如果有子目录则不删除
os.Remove(filepath.Join(path, "newfolder/dic"))
}