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 (
          "fmt"
          "os"
          "path/filepath"
        )

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

          // Path to executable file
          fmt.Println(ex)

          // 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 {
            panic(err)
          }
          fmt.Println("Symlink evaluated:" + realPath)
        }



output:

Biradars-MacBook-Air-4:golang-daily sangam$ go build -o binary
Biradars-MacBook-Air-4:golang-daily sangam$ ./binary 
/Users/sangam/Documents/golang-daily/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…