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
-
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.
-
How to get the current working directory in Golang?
In Golang, Getwd is the function to get the current working directory.
-
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!”