Golang Program to Find the current working directory


In go language we can use the functions present in os package like Getwd() and Args to find the current directory in which our code is getting executed.

The directory from which the program is currently being run is called the current directory. It is also known as present working directory.

The directory in which the program is currently running is called the working directory. It is the parent directory for any files or the or directory that are created during runtime.

Algorithm

  • First, we need to import the "fmt" and "os" packages.

  • Then, start the main() function. Inside the main() call the required method present in the respective package.

  • Check if there is an error by checking if "err" is not nil

  • If there is an error, print the error and stop the further execution of the program.

  • If there is no error, print the current working directory, which is stored in the "dir" variable.

Syntax

funcGetwd() (dir string, err error)

The Getwd() function is present in os package and is used to get the rooted path of a particular directory. The function returns two values. One is the string variable containing the path length of directory and other is the error message. The error is not null if there is some problem in getting the required result.

funcDir(path string) string

The Dir() function is present in the filepath package and is used to return all the elements of the specified path except the last element. The function accepts one argument which is the path length of the directory and returns all the elements of the specified path except the last element.

funcAbs(path string) (string, error)

The Abs() function is present in path package and is used to return an absolute representation of specified path. If the path is not absolute it will be joined with the current working directory to turn it into an absolute path. The function accepts the specified path as an argument and returns the absolute representation of the specified path along with the error.

Example 1

In this Example we will write a go language program to find the current working directory by using the Getwd() function present in os package.

package main

import (
   "fmt"
   "os"
)

func main() {
   dir, err := os.Getwd()
   if err != nil {
      fmt.Println(err)
      return
   }
   fmt.Println("Current working directory:", dir)
}

Output

Current working directory: C:\Users\LENOVO\Desktop\go

Example 2

Another way to find the current working directory in Golang is by using the os.Args[0] value. This value represents the name of the executable program. By using this value, we can find the current working directory of the program by removing the program name from the full path of the program.

package main

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

func main() {
   dir := filepath.Dir(os.Args[0])
   fmt.Println("Current working directory:", dir)
}

Output

Current working directory: C:\Users\LENOVO\AppData\Local\Temp\go-build3596082773\b001\exe

Example 3

We can also use other methods to find the current working directory in Golang by using the filepath.Abs and filepath.Dir functions from the path/filepath package. The filepath.Abs function returns the absolute path of a file, and the filepath.Dir function returns the directory of a file. We can use these two functions together to find the current working directory of the Go program.

package main

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

func main() {
   dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
   if err != nil {
      fmt.Println(err)
      return
   }
   fmt.Println("Current working directory:", dir)
}

Output

Current directory: C:\Users\LENOVO\AppData\Local\Temp\go-build514274184\b001\exe

Conclusion

We have successfully compiled and executed a go language program to find the current working directory along with Examples. Here we have discussed three different methods for finding the current working directory in Golang. Each method has its own advantages and disadvantages, but the most straightforward and easiest method is the first one, using os.Getwd. It's up to the developer to choose the method that best fits their needs.

Updated on: 03-May-2023

301 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements