51 lines
960 B
Go
51 lines
960 B
Go
package HtmlTools
|
||
|
||
import (
|
||
"log"
|
||
"os"
|
||
"path"
|
||
"path/filepath"
|
||
"runtime"
|
||
"strings"
|
||
)
|
||
|
||
// see https://zhuanlan.zhihu.com/p/363714760
|
||
// 最终方案-全兼容
|
||
func GetCurrentAbPath() string {
|
||
dir := getCurrentAbPathByExecutable()
|
||
if strings.Contains(dir, getTmpDir()) {
|
||
return getCurrentAbPathByCaller()
|
||
}
|
||
return dir
|
||
}
|
||
|
||
// 获取系统临时目录,兼容go run
|
||
func getTmpDir() string {
|
||
dir := os.Getenv("TEMP")
|
||
if dir == "" {
|
||
dir = os.Getenv("TMP")
|
||
}
|
||
res, _ := filepath.EvalSymlinks(dir)
|
||
return res
|
||
}
|
||
|
||
// 获取当前执行文件绝对路径
|
||
func getCurrentAbPathByExecutable() string {
|
||
exePath, err := os.Executable()
|
||
if err != nil {
|
||
log.Fatal(err)
|
||
}
|
||
res, _ := filepath.EvalSymlinks(filepath.Dir(exePath))
|
||
return res
|
||
}
|
||
|
||
// 获取当前执行文件绝对路径(go run)
|
||
func getCurrentAbPathByCaller() string {
|
||
var abPath string
|
||
_, filename, _, ok := runtime.Caller(0)
|
||
if ok {
|
||
abPath = path.Dir(filename)
|
||
}
|
||
return abPath
|
||
}
|