How to get the directory of the currently running file?

后端 未结 10 525
日久生厌
日久生厌 2020-11-30 16:38

In nodejs I use __dirname . What is the equivalent of this in Golang?

I have googled and found out this article http://andrewbrookins.com/tech/golang-get-directory-o

10条回答
  •  Happy的楠姐
    2020-11-30 17:34

    This should do it:

    import (
        "fmt"
        "log"
        "os"
        "path/filepath"
    )
    
    func main() {
        dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
        if err != nil {
                log.Fatal(err)
        }
        fmt.Println(dir)
    }
    

提交回复
热议问题