Golang program to get current working directory


Golang has a large range of internal packages to work with the directory. The directory in which the application is operating is known as the current working directory. Here we can use the OS packages as well as the path/filepath package of Golang to get current working directory. In the OS package we can use os.Getwd() function to the current working director. Whereas in path/filepath package we can use filepath.Abs and filepath.dir to get the filepath of the current working directory.

Method 1: Using os.Getwd() function

This method uses the os.Getwd function from the os package to get the current working directory. If there is a problem, the error message is printed to the standard output. Otherwise, the normal output prints the current working directory. Have a ook at the code and the algorithm to understand it.

Syntax

os.Getwd()

In Go programming language, the current working directory of the process can be obtained using the os.Getwd function. The function gives back as a string the absolute path to the current working directory.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package), os package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − In this step use os.Getwd() function to get the current working directory.

  • Step 3 − If the err!=nil, this implies that there is an error while getting current working directory.

  • Step 4 − If no error occurs a current working directory will be printed on the console.

  • Step 5 − The print statement will be executed on the console using fmt.Println() function where ln means new line.

Example

In this example, we will use os package’s os.Getwd function to execute the program.

package main
import (
   "fmt"
   "os"    //import fmt and os package
)

func main() {
   directory, err := os.Getwd()    //get the current directory using the built-in function
   if err != nil {
      fmt.Println(err) //print the error if obtained
   }
   fmt.Println("Current working directory:", directory) //print the required directory
}

Output

Current working directory: /Users/username/go/src/example

Method 2: Using path/filepath package

In this illustration, the filepath.Abs and filepath.Dir methods are used to find the directory's absolute path where the active executable file is located. These routines with the os.Args[0] value, which denotes the name of the currently running executable file, can be used to determine the current working directory.

Syntax

filepath.Abs

In Go, the filepath.Abs function returns an absolute representation of a given file path. It takes a single argument, a string representing a file path, and returns an absolute representation of the path, resolved relative to the current working directory.

filepath.dir

The filepath.Dir method in Go returns a file path's directory component. It just needs one input, a string that represents a file path, and it outputs the file path's leading directory component.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package), os package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Call the filepath.Abs function with filepath.Dir function as an input in the function is assigned to the directory.

  • Step 3 − If no error is accompanied, current working directory is printed on the console.

  • Step 4 − If error is discovered, it will be logged on the console using log.Fatal.

Example

In this example, we are going to use path/filepath package to get the current working directory in Golang.

package main
import (
   "log"
   "os"
   "path/filepath"
)

func main() {
   directory, err := filepath.Abs(filepath.Dir(os.Args[0]))  //get the current working directory
   if err != nil {
      log.Fatal(err)   //print the error if obtained
   }
   log.Println("Current working directory:", directory) //print the current working directory
}

Output

Current working directory: /Users/username/go/src/example

Conclusion

We executed the program of getting the current working directory using two examples. In the first example we used os.Getwd() function and in the second example we used path/filepath package to execute the program.

Updated on: 22-Feb-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements