1. package main
    2. import (
    3. "fmt"
    4. "os"
    5. "path/filepath"
    6. )
    7. // 查看执文件所在目录
    8. func getExecutePathByDir() string {
    9. dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
    10. if err != nil {
    11. fmt.Println(err)
    12. }
    13. //fmt.Println(dir)
    14. return dir
    15. }
    16. func getExecutePathByAbs() string {
    17. dir, err := filepath.Abs("./")
    18. if err != nil {
    19. fmt.Println(err)
    20. }
    21. //fmt.Println(dir)
    22. return dir
    23. }
    24. func getExecutePathByGetwd() string {
    25. dir, err := os.Getwd()
    26. if err != nil {
    27. fmt.Println(err)
    28. }
    29. //fmt.Println(dir)
    30. return dir
    31. }
    32. func getExecutePathByExecutable() string {
    33. dir, err := os.Executable()
    34. if err != nil {
    35. fmt.Println(err)
    36. }
    37. fmt.Println("Executable:", dir)
    38. exPath := filepath.Dir(dir)
    39. fmt.Println("exPath:", dir)
    40. return exPath
    41. }
    42. func main() {
    43. fmt.Println("getExecutePathByDir:", getExecutePathByDir())
    44. fmt.Println("getExecutePathByAbs:", getExecutePathByAbs())
    45. fmt.Println("getExecutePathByGetwd:", getExecutePathByGetwd())
    46. fmt.Println("getExecutePathByExecutable:", getExecutePathByExecutable())
    47. }
    48. //https://www.cnblogs.com/-wenli/p/12026413.html