Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Directory Definition
3.
Create a Directory
3.1.
Example
4.
Creating Multiple Directories
4.1.
Example
5.
Check if the Directory exists
5.1.
Example
6.
Deleting a Directory
6.1.
Example
7.
FAQs
8.
Key Takeaways
Last Updated: Mar 27, 2024

Go Directories

Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

This blog will cover the Go Directories while moving to the topic. Let's brush up few concepts, like what a directory is. A directory is simply a folder in windows, where we keep all the files. We can create, delete and rename them according to our needs. 

Directory Definition

A directory can be defined as a unit in a computer's file system used to store and locate files. Directories are hierarchically organized into a tree. Directories have a parent-child relationship. Directories are also referred to as folders.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Create a Directory

The os.Mkdir creates a new directory with the specified name and permissible bits.

Example

package main
 
import (
    "log"
    "os"
    "fmt"
)
 
func main() {
 
    err := os.Mkdir("tmp", 0755)
    fmt.Print("The Directory has been created")
    if err != nil {
        log.Fatal(err)
    }
}

Output

The directory has been created.

In this example, we have created a directory named tmp, and if this creation is successful, it will print the correct message; otherwise, it will show some error messages. The code 0755 means it has to read and execute access for everyone and the user.

Creating Multiple Directories

The MkdirAll function creates a directory, along with necessary parents, and it returns nil or else an error message. 

Example

package main
 
import (
    "fmt"
    "log"
    "os"
)
 
func main() {
 
    path := "tmp/doc/new"
 
    err := os.MkdirAll(path, 0755)
 
    if err != nil {
        log.Fatal(err)
    }
 
    fmt.Println("Directory created")
}

Output

Directory created

In this example, we have created multiple directories with the specified path. If the creation is successful, it will show the correct message. Otherwise, it will show an error message.

Check if the Directory exists

The function IsNotExist helps us to check whether a particular directory is present or not. 

Example

package main
 
import (
    "fmt"
    "os"
)
 
func main() {
 
    path := "tmp"
 
    if _, err := os.Stat(path); os.IsNotExist(err) {
 
        os.Mkdir(path, 0755)
        fmt.Println("Directory created")
    } else {
 
        fmt.Println("Directory already exists")
    }
}

Output

Directory already exists

In this example, we have checked whether a particular directory exists or not. We checked certain conditions and returned the message after getting the directory.

Deleting a Directory

The RemoveAll function removes the directory and its content.

Example

package main
 
import (
    "fmt"
    "log"
    "os"
)
 
func main() {
 
    err := os.RemoveAll("tmp")
    fmt.Print("Remove Successful")
    if err != nil {
        log.Fatal(err)
    }
}

Output

Remove Successful

In this example, we have removed a directory using the in-built function of GO.

FAQs

  1. Why do we create directories in Go?
    Creating a directory automatically grants the Read and Writes privileges on the directory, and you can grant these permissions to other users and roles.
     
  2. How to get the current working directory in Golang?
    In Golang, Getwd is the function to get the current working directory.
     
  3. How to list all directories in Go?
    In Golang, we can list all directories with the help of ioutil.ReadDir, filepath.Glob.

Key Takeaways

In this article we have discussed Go Directories and their implementation in GoLang. We briefly introduced the topic and gave the basic definition of the topic. We also took how to create directories and many more operations on directories. We also took several examples to show these operations.

If you want to practice some interview questions, visit here Top Uber Questions.

We hope that this blog has helped you enhance your knowledge regarding Go Directories and if you would like to learn more, check out our articles on Golang Archives.

Do upvote our blog to help other ninjas grow. Happy Coding!”

Next article
Go Reading & Writing Files
Live masterclass