Golang program to create directories


Golang has internal packages like os and io packages for creating a new directory. Here, we will create a directory using two examples. In the first example we will use os.Mkdir function and in the second example we will use ioutil.WriteFile function to execute the program.

Method 1: Using os.Mkdir Function

In this method, a directory named dirName variable is created using the os.Mkdir function. The permission bits for the new directory are the second input to os.Mkdir, which we set to 0755. (read, write, and execute permissions for the owner and read and execute permissions for others). The program will print "Directory created successfully!" if the directory creation is successful otherwise it will produce an error message.

Syntax

os.Mkdir()

The os.Mkdir function in Go helps in creating a new directory with the specified name and permission bits (mode).

Algorithm

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

  • Step 2 − Create a directoryname variable and assign it to the newdir one wants to create.

  • Step 3 − Use os.Mkdir function to create a new directory.

  • Step 4 − If an error persists while creating the directory, print the error on the console using fmt.Println() function where ln means new line and return.

  • Step 5 − If the directory is created successfully, Print the success message using the statement used in Step4.

Example

In this example we will use os.Mkdir function to create new directory.

package main
import (
   "fmt"
   "os" //import fmt and os package 
)
//create main function to execute the program 
func main() {
   directoryname := "newdir"
   err := os.Mkdir(directoryname, 0755) //create a directory and give it required permissions
   if err != nil {
      fmt.Println(err) //print the error on the console
      return
   }
   fmt.Println("Directory created successfully!") //print the success message if directory is created successfully
}

Output

Directory created successfully!

Method 2: Using io/ioutil Package

In this method, an empty file with the name and permission bits is created using the ioutil.WriteFile function. If the function returns a nil error and the file is successfully created, we will print success. The WriteFile function will create the directory and if an error comes while creating a directory, we will print the error message.

Syntax

Ioutil.WriteFile()

The ioutil.WriteFile function in Go is used to write a byte slice to a file.

Algorithm

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

  • Step 2 − Create the function main and in that function create a variable directoryname and assign it to newdir.

  • Step 3 − Use ioutil package function iouti.WriteFile function to create a directory.

  • Step 4 − If an error comes while creating the directory, print the error on the console and return.

  • Step 5 − If the directory is created successfully, print the success statement on the console.

  • Step 6 − The print statement is executed fmt.Println() function where ln means new line.

Example

In this example, we will use io/ioutil package function to create new directory.

package main
import (
   "fmt"
   "io/ioutil"  //import fmt and io/ioutil package
)

//create main function to execute the program
func main() {
   directoryname := "newdir"
   err := ioutil.WriteFile(directoryname, []byte(""), 0755) //create new directory and give it permissions 
   if err != nil {
      fmt.Println(err)     //print the error if there is an error persisting
      return
   }
   fmt.Println("Directory created successfully!") //if directory is created successfully print success
}

Output

Directory created successfully!

Conclusion

We executed the program of creating a directory using two methods. In the first method we used os.Mkdir function and in the second method we used io/ioutil package to execute the program.

Updated on: 22-Feb-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements