Golang Program to Create directories recursively


In this golang article we will write a program to create directories recursively using os.MkdirAll() function as well as using recursion. One of the main use cases of Go is to create scalable and concurrent applications.

Method 1

In this Example we will write a go language program to create directories recursively by using MkdirAll() function present in os package. The simplest way to create directories recursively in Golang is by using this function. This function takes a file path as an argument and creates all the missing directories in the path.

Syntax

funcMkdirAll(path string, perm FileMode) error

The mkdirAll() function is present in os package and is used to create a directory with respective parents. The function accepts two arguments one is the name by which the directory should be created. The function returns an error if there is some problem in creating the directory.

Algorithm

  • First, we need to import the os and fmt package in your Go code.

  • Then, start the main() function. Inside the main() call the os.MkdirAll function, passing in the file path and the desired permissions (in octal) as arguments.

  • Check the return value of the os.MkdirAll function. If it is not equal to nil, then an error occurred while creating the directories.

  • If the return value is equal to nil, then the directories have been created successfully.

Example 1

The following Example will create directories recursively by using os.mkdirall function in golang.

package main

import (
   "fmt"
   "os"
)

func main() {
   err := os.MkdirAll("/newone/again", 0777)
   if err != nil {
      fmt.Println(err)
      return
   }
   fmt.Println("Directory hierarchy created successfully")
}

Output

Directory hierarchy created successfully

Method 2

Another way to create directories recursively in Golang is to use the os.Mkdir function along with recursion. In this method, we create a recursive function that takes a file path as an argument and creates the directories one by one.

Algorithm

  • First, we need to import the os and path/filepath packages in your Go code.

  • Then start the main() function. Inside the main() define a recursive function createDirectory that takes a file path as an argument.

  • Inside the createDirectory function, use the os.Stat function to check if the directory already exists.

  • If the directory exists, stop the further execution of the function.

  • If the directory does not exist, use the os.IsNotExist function to check the error returned by os.Stat.

  • If the error is os.IsNotExist, then call the createDirectory function again, passing in the parent directory of the current directory.

  • After the parent directory has been created, use the os.Mkdir function to create the current directory.

  • Return nil if the os.Mkdir call is successful, or the error returned by os.Mkdir otherwise.

  • Call the createDirectory function, passing in the file path, to start the directory creation process.

  • Check the return value of the createDirectory function. If it is not equal to nil, then an error occurred while creating the directories.

  • If the return value is equal to nil, then the directories have been created successfully.

Example

The following Example is the go language program to create directories recursively by using usingos.mkdir and recursion

package main

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

func createDirectory(path string) error {
   // Check if the directory exists
   _, err := os.Stat(path)
   if err == nil {
      return nil
   }
   // If the directory does not exist, create its parent
   if os.IsNotExist(err) {
      err = createDirectory(filepath.Dir(path))
      if err != nil {
         return err
      }
      // Create the directory
      err = os.Mkdir(path, 0777)
      if err != nil {
         return err
      }
   }
   return nil
}

func main() {
   err := createDirectory("/tmp/a/b/c")
   if err != nil {
      fmt.Println(err)
      return
   }
   fmt.Println("Directory hierarchy created successfully")
}

Output

Directory hierarchy created successfully

Conclusion

We have successfully compiled and executed a go language program to create the directories recursively along with Examples. In this article, we have explored two methods to create directories recursively in Golang. The first method uses the os.MkdirAll() function, while the second method uses the os.Mkdir() function along with recursion. Both methods are simple and straightforward to use and allow you to create directory hierarchies in Golang easily.

Updated on: 03-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements