How to get the directory of the currently running file?
Tags: go
Published: 2020-05-22 12:57:14

The <a href="https://golang.org/">Golang</a> programming language makes it easy to build simple, reliable, and efficient software.  

You can get the current directory of the program you are running (<a href="https://golangr.com/hello-world/">run golang</a>). 

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

```go
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:

```go
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:

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