Go 获取程序运行的绝对路径

package main

import (
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
)

func main() {
    fmt.Println(GetCurrPath())
}

func GetCurrPath() string {
    file, _ := exec.LookPath(os.Args[0])
    path, _ := filepath.Abs(file)
    index := strings.LastIndex(path, string(os.PathSeparator))
    ret := path[:index]
    return ret
}

相关文章