How to get the directory of the currently running file?

后端 未结 10 532
日久生厌
日久生厌 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条回答
  •  臣服心动
    2020-11-30 17:19

    dir, err := os.Getwd()
        if err != nil {
            fmt.Println(err)
        }
    

    this is for golang version: go version go1.13.7 linux/amd64

    works for me, for go run main.go. If I run go build -o fileName, and put the final executable in some other folder, then that path is given while running the executable.

提交回复
热议问题