Files
ngzz-mc/internal/paths/paths.go

34 lines
752 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package paths 资源路径解析:全部相对路径基于「可执行文件所在目录」解析,
// 保证双击运行 / 桌面快捷方式 / 安装目录下任意 CWD 都能找到资源(修复闪退根因)。
package paths
import (
"os"
"path/filepath"
"sync"
)
var (
once sync.Once
baseDir string
)
// BaseDir 可执行文件所在目录(缓存;失败回退 CWD
func BaseDir() string {
once.Do(func() {
exe, err := os.Executable()
if err != nil {
baseDir, _ = os.Getwd()
return
}
baseDir = filepath.Dir(exe)
})
return baseDir
}
// Join 基于可执行文件目录拼接相对路径。
func Join(rel ...string) string {
parts := append([]string{BaseDir()}, rel...)
return filepath.Join(parts...)
}