Go语言获取项目当前路径

package main

import (
    "fmt"
    "os"
    "os/exec"
    "strings"
)

func main() {
    path := getCurrentPath()
    fmt.Println(path)
}

func getCurrentPath() string {
    s, err := exec.LookPath(os.Args[0])
    checkErr(err)
    i := strings.LastIndex(s, "\\")
    path := string(s[0 : i+1])
    return path
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

 

posted @ 2016-12-01 14:39  ~风轻云淡~  阅读(20066)  评论(0编辑  收藏  举报