From 9f128533334c7ffa5b91c24c88d9c07a9945aeda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Fri, 13 Jan 2023 11:59:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=E6=96=87=E4=BB=B6=E5=89=8D?= =?UTF-8?q?=EF=BC=8C=E5=88=9B=E5=BB=BA=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/file.go b/file.go index 219d4e2..dfee1c3 100644 --- a/file.go +++ b/file.go @@ -13,6 +13,7 @@ import ( "errors" "io" "os" + "path/filepath" "strings" "gopkg.in/yaml.v3" @@ -144,6 +145,14 @@ func (f *fileOperate) Create() error { return errors.New("文件不存在, 且不允许自动创建 : " + f.filePath) } } + // 创建目录 + fileArr := strings.Split(f.filePath, string(filepath.Separator)) + if len(fileArr) >= 2 { + pathStr := strings.Join(fileArr[0:len(fileArr)-1], string(filepath.Separator)) + if err = os.MkdirAll(pathStr, 0777); nil != err { + return errors.New("文件目录创建失败 : " + err.Error()) + } + } f.fileHandler, err = os.OpenFile(f.filePath, f.openFlag, f.permission) return err }