From 1f6145405ec403e0d8fdf7e149527277fae56763 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 3 Jan 2026 22:27:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=88=A4=E6=96=AD?= =?UTF-8?q?=E6=98=AF=E5=90=A6=20go=20run=20=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/project.go b/project.go index 80d502d..4799352 100644 --- a/project.go +++ b/project.go @@ -36,6 +36,10 @@ func (p *project) GetFileSystemSeparator() string { // BuildPath 构建路径 func (p *project) BuildPath(filePathList ...string) string { projectRootPath, _ := p.GetExecutablePath() + if p.IsRunningWithGoRun() { + // go run 运行, 则工作目录为项目目录 + projectRootPath = p.GetWorkDir() + } if len(filePathList) == 0 { // 降为获取项目根目录 return projectRootPath @@ -68,3 +72,29 @@ func (p *project) GetHomeDir() string { homePath, _ := homedir.Dir() return homePath } + +// IsRunningWithGoRun 获取程序是否通过 go run 运行 +func (p *project) IsRunningWithGoRun() bool { + // 方法1: 检查执行文件路径是否包含临时目录特征 + exePath, err := os.Executable() + if err != nil { + return false + } + + // go run 会在临时目录创建二进制文件 + // 不同系统的临时目录特征: + switch runtime.GOOS { + case "windows": + // Windows: 通常在 C:\Users\\AppData\Local\Temp\go-build\ + return strings.Contains(exePath, "go-build") || + strings.Contains(exePath, `\go-build\`) || + strings.Contains(filepath.Base(exePath), "go-build") + case "darwin", "linux": + // macOS/Linux: 通常在 /tmp/go-build/ + return strings.Contains(exePath, "/go-build/") || + strings.Contains(exePath, "/go-build") || + (strings.HasPrefix(exePath, "/var/folders/") && strings.Contains(exePath, "/go-build/")) + } + + return false +}