可以使用os.Executable()函数获取当前运行程序的完整路径,然后使用filepath.Dir()函数获取该路径所在的目录。

示例代码:

package main

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

func main() {
    exePath, err := os.Executable()
    if err != nil {
        panic(err)
    }
    exeDir := filepath.Dir(exePath)
    fmt.Println(exeDir)
}

输出:

/Users/username/go/src/example
golang取运行的目录

原文地址: http://www.cveoy.top/t/topic/fVhc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录