package main
import (
"fmt"
"os"
"path/filepath"
)
// 查看执文件所在目录
func getExecutePathByDir() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
fmt.Println(err)
}
//fmt.Println(dir)
return dir
}
func getExecutePathByAbs() string {
dir, err := filepath.Abs("./")
if err != nil {
fmt.Println(err)
}
//fmt.Println(dir)
return dir
}
func getExecutePathByGetwd() string {
dir, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
//fmt.Println(dir)
return dir
}
func getExecutePathByExecutable() string {
dir, err := os.Executable()
if err != nil {
fmt.Println(err)
}
fmt.Println("Executable:", dir)
exPath := filepath.Dir(dir)
fmt.Println("exPath:", dir)
return exPath
}
func main() {
fmt.Println("getExecutePathByDir:", getExecutePathByDir())
fmt.Println("getExecutePathByAbs:", getExecutePathByAbs())
fmt.Println("getExecutePathByGetwd:", getExecutePathByGetwd())
fmt.Println("getExecutePathByExecutable:", getExecutePathByExecutable())
}
//https://www.cnblogs.com/-wenli/p/12026413.html