Introduction
Creating a directory is a common operation in file handling. In Go, this can be easily achieved using the os package. This guide will demonstrate how to write a Go program that creates a directory.
Problem Statement
Create a Go program that:
- Takes the directory name as input.
- Creates the directory if it does not already exist.
- Displays a message indicating whether the directory was successfully created or if an error occurred.
Example:
- Input: Directory name
"mydir" - Output:
Directory created successfully.or
Error creating directory: directory already exists.
Solution Steps
- Import the Necessary Packages: Use
import "fmt"andimport "os"for file operations and formatted I/O. - Write a Function to Create the Directory: Implement a function that creates the directory using
os.Mkdiroros.MkdirAll. - Write the Main Function: Define the
mainfunction, which is the entry point of every Go program. - Prompt the User to Enter the Directory Name: Use
fmt.Scanlnorfmt.Scanfto take the directory name as input. - Call the Function to Create the Directory: Use the function to create the directory.
- Display the Result: Use
fmt.Printlnto display whether the directory was successfully created or if an error occurred.
Go Program
package main
import (
"fmt"
"os"
)
// Step 2: Implement a function to create the directory
func createDirectory(dirname string) error {
// Use os.Mkdir or os.MkdirAll to create the directory
// os.Mkdir creates a single directory, while os.MkdirAll creates all necessary parent directories as well
err := os.Mkdir(dirname, 0755)
if err != nil {
return err
}
return nil
}
/**
* Go Program to Create a Directory
* Author: https://www.javaguides.net/
*/
func main() {
// Step 4: Prompt the user to enter the directory name
var dirname string
fmt.Print("Enter the directory name: ")
fmt.Scanln(&dirname)
// Step 5: Call the function to create the directory
err := createDirectory(dirname)
if err != nil {
fmt.Println("Error creating directory:", err)
} else {
fmt.Println("Directory created successfully.")
}
}
Explanation
Step 2: Implement a Function to Create the Directory
- The
createDirectoryfunction usesos.Mkdirto create the directory:os.Mkdircreates a single directory with the specified name (dirname).- The second argument,
0755, sets the permissions for the directory. This allows the owner to read, write, and execute, while others can only read and execute. - If the directory cannot be created (e.g., it already exists), an error is returned.
Step 3: Write the Main Function
- The
mainfunction prompts the user for the directory name and callscreateDirectoryto create the directory.
Step 4: Prompt the User to Enter the Directory Name
- The program prompts the user to enter the directory name using
fmt.Printand reads the input usingfmt.Scanln.
Step 5: Call the Function to Create the Directory
- The program calls
createDirectorywith the provided directory name to create the directory.
Step 6: Display the Result
- The program prints a message indicating whether the directory was successfully created or if an error occurred using
fmt.Println.
Output Example
Example 1:
Enter the directory name: mydir
Directory created successfully.
Example 2:
Enter the directory name: existingdir
Error creating directory: mkdir existingdir: file exists
Example 3 (Empty input):
Enter the directory name:
Error creating directory: mkdir : no such file or directory
Conclusion
This Go program demonstrates how to create a directory using the os package. It covers basic file operations such as directory creation and error handling in Go. This example is useful for beginners learning Go programming and understanding how to perform directory manipulation tasks effectively.