From dfc411b9c6e8ea51f96f3cb384f7865f2f4b4ace Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Thu, 15 Jun 2023 16:17:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=B8=AAlogic?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E4=B8=8D=E8=A6=86=E7=9B=96=E7=9A=84=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- generator/main.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/generator/main.go b/generator/main.go index bef23656..61aa61fb 100644 --- a/generator/main.go +++ b/generator/main.go @@ -142,16 +142,26 @@ func GenFromPath(pth string) { } fcontent = "package model\n// TODO: 使用model的属性做你想做的" - genGoFileName = fmt.Sprintf("%s/%s_logic.go", genDir, table.Name) - f2, err := os.OpenFile(genGoFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) - if err != nil { - panic(err) - } - f2.WriteString(fcontent) - err = f2.Close() - if err != nil { - panic(err) + genGoLogicFileName := fmt.Sprintf("%s/%s_logic.go", genDir, table.Name) + + // 使用 os.Stat 函数获取文件信息 + _, err = os.Stat(genGoLogicFileName) + // 判断文件是否存在并输出结果 + if os.IsNotExist(err) { + f2, err := os.OpenFile(genGoLogicFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644) + if err != nil { + panic(err) + } + f2.WriteString(fcontent) + err = f2.Close() + if err != nil { + panic(err) + } + fmt.Println(genGoLogicFileName, "create!") + } else { + fmt.Println(genGoLogicFileName, "exists") } + } }