DEV Community

Cover image for How to get the directory of the currently running file?
natamacm
natamacm

Posted on

How to get the directory of the currently running file?

#go

The Golang programming language makes it easy to build simple, reliable, and efficient software.

You can get the current directory of the program you are running (run golang).

So how to find out where the executable is?

As usual in programming languages, there are several ways to do this.
It's not Go specific, as you can have the same problem when programming C or C++.

To get the directory of the executable you can use "path/filepath.Dir".

For Go 1.8 and above you can use os.Executable for getting executable path

package main                                                                                                                               

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

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

An alternative way to do this is with this code, it returns the current working directory:

package main

import (
    "fmt"
    "os"
)

func main() {
    pwd, err := os.Getwd()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println(pwd)
}

You can also do it this way:

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)
}

Top comments (0)