[转载]Golang 获取当前可执行文件的dir

package common

import (
    "errors"
    "os"
    "os/exec"
    "path/filepath"
    "runtime"
    "strings"
)

func GetCurrentPath() (string, error) {
    file, err := exec.LookPath(os.Args[0])
    if err != nil {
        return "", err
    }
    path, err := filepath.Abs(file)
    if err != nil {
        return "", err
    }
    //fmt.Println("path111:", path)
    if runtime.GOOS == "windows" {
        path = strings.Replace(path, "\\", "/", -1)
    }
    //fmt.Println("path222:", path)
    i := strings.LastIndex(path, "/")
    if i < 0 {
        return "", errors.New(`can't find "/" or "\"`)
    }
    //fmt.Println("path333:", path)
    return string(path[0 : i+1]), nil
}

原文链接


好好学习,天天向上