Go Program to Create a Directory

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

  1. Import the Necessary Packages: Use import "fmt" and import "os" for file operations and formatted I/O.
  2. Write a Function to Create the Directory: Implement a function that creates the directory using os.Mkdir or os.MkdirAll.
  3. Write the Main Function: Define the main function, which is the entry point of every Go program.
  4. Prompt the User to Enter the Directory Name: Use fmt.Scanln or fmt.Scanf to take the directory name as input.
  5. Call the Function to Create the Directory: Use the function to create the directory.
  6. Display the Result: Use fmt.Println to 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 createDirectory function uses os.Mkdir to create the directory:
    • os.Mkdir creates 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 main function prompts the user for the directory name and calls createDirectory to 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.Print and reads the input using fmt.Scanln.

Step 5: Call the Function to Create the Directory

  • The program calls createDirectory with 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top