How to get the directory of the currently running file?

后端 未结 10 517
日久生厌
日久生厌 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:11

    os.Executable: https://tip.golang.org/pkg/os/#Executable

    filepath.EvalSymlinks: https://golang.org/pkg/path/filepath/#EvalSymlinks

    Full Demo:

    package main
    
    import (
        "fmt"
        "os"
        "path/filepath"
    )
    
    func main() {
        var dirAbsPath string
        ex, err := os.Executable()
        if err == nil {
            dirAbsPath = filepath.Dir(ex)
            fmt.Println(dirAbsPath)
            return
        }
    
        exReal, err := filepath.EvalSymlinks(ex)
        if err != nil {
            panic(err)
        }
        dirAbsPath = filepath.Dir(exReal)
        fmt.Println(dirAbsPath)
    }
    
    0 讨论(0)
  • 2020-11-30 17:14

    Gustavo Niemeyer's answer is great. But in Windows, runtime proc is mostly in another dir, like this:

    "C:\Users\XXX\AppData\Local\Temp"
    

    If you use relative file path, like "/config/api.yaml", this will use your project path where your code exists.

    0 讨论(0)
  • 2020-11-30 17:17

    If you use package osext by kardianos and you need to test locally, like Derek Dowling commented:

    This works fine until you'd like to use it with go run main.go for local development. Not sure how best to get around that without building an executable beforehand each time.

    The solution to this is to make a gorun.exe utility instead of using go run. The gorun.exe utility would compile the project using "go build", then run it right after, in the normal directory of your project.

    I had this issue with other compilers and found myself making these utilities since they are not shipped with the compiler... it is especially arcane with tools like C where you have to compile and link and then run it (too much work).

    If anyone likes my idea of gorun.exe (or elf) I will likely upload it to github soon..

    Sorry, this answer is meant as a comment, but I cannot comment due to me not having a reputation big enough yet.

    Alternatively, "go run" could be modified (if it does not have this feature already) to have a parameter such as "go run -notemp" to not run the program in a temporary directory (or something similar). But I would prefer just typing out gorun or "gor" as it is shorter than a convoluted parameter. Gorun.exe or gor.exe would need to be installed in the same directory as your go compiler

    Implementing gorun.exe (or gor.exe) would be trivial, as I have done it with other compilers in only a few lines of code... (famous last words ;-)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-30 17:20

    Sometimes this is enough, the first argument will always be the file path

    package main
    
    import (
        "fmt"
        "os"
    )
    
    
    func main() {
        fmt.Println(os.Args[0])
    
        // or
        dir, _ := os.Getwd()
        fmt.Println(dir)
    }
    
    0 讨论(0)
  • 2020-11-30 17:21
    filepath.Abs("./")
    

    Abs returns an absolute representation of path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path.

    As stated in the comment, this returns the directory which is currently active.

    0 讨论(0)
提交回复
热议问题