Retrieving the current working directory

Another useful source of information for the application is the directory, where the program binary is located. With this information, the program can access the assets and files collocated with the binary file.

Note: Go since version 1.8. This one is the preferred one.

Create the main.go file with the following content:

        package main

        import (

        func main() {
          ex, err := os.Executable()
          if err != nil {

          // Path to executable file

          // Resolve the direcotry
          // of the executable
          exPath := filepath.Dir(ex)
          fmt.Println("Executable path :" + exPath)

          // Use EvalSymlinks to get
          // the real path.
          realPath, err := filepath.EvalSymlinks(exPath)
          if err != nil {
          fmt.Println("Symlink evaluated:" + realPath)


Biradars-MacBook-Air-4:golang-daily sangam$ go build -o binary
Biradars-MacBook-Air-4:golang-daily sangam$ ./binary 
Executable path :/Users/sangam/Documents/golang-daily
Symlink evaluated:/Users/sangam/Documents/golang-daily
Biradars-MacBook-Air-4:golang-daily sangam$ 

How it works…