Directories
// Importing necessary packages.
import (
"fmt"
"io/fs"
"os"
"path/filepath"
)
// Function to check and panic if an error occurs.
func check(e error) {
if e != nil {
panic(e)
}
}
// Main function where the execution of the program begins.
func main() {
// Creating a new directory "subdir" with permissions 0755.
err := os.Mkdir("subdir", 0755)
check(err)
// Defer removing the "subdir" directory at the end of the program.
defer os.RemoveAll("subdir")
// Function to create an empty file with a given name.
createEmptyFile := func(name string) {
d := []byte("")
check(os.WriteFile(name, d, 0644))
}
// Creating an empty file "subdir/file1".
createEmptyFile("subdir/file1")
// Creating nested directories and files.
err = os.MkdirAll("subdir/parent/child", 0755)
check(err)
createEmptyFile("subdir/parent/file2")
createEmptyFile("subdir/parent/file3")
createEmptyFile("subdir/parent/child/file4")
// Reading the contents of the "subdir/parent" directory.
c, err := os.ReadDir("subdir/parent")
check(err)
fmt.Println("Listing subdir/parent")
for _, entry := range c {
// Printing the name and whether it's a directory or not.
fmt.Println(" ", entry.Name(), entry.IsDir())
}
// Changing the current working directory to "subdir/parent/child".
err = os.Chdir("subdir/parent/child")
check(err)
// Reading the contents of the current working directory.
c, err = os.ReadDir(".")
check(err)
fmt.Println("Listing subdir/parent/child")
for _, entry := range c {
// Printing the name and whether it's a directory or not.
fmt.Println(" ", entry.Name(), entry.IsDir())
}
// Changing the current working directory back to the root.
err = os.Chdir("../../..")
check(err)
// Using filepath.WalkDir to visit all files and directories in "subdir".
fmt.Println("Visiting subdir")
err = filepath.WalkDir("subdir", visit)
check(err)
}
// Function to be called during the filepath.WalkDir traversal.
func visit(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// Printing the path and whether it's a directory or not.
fmt.Println(" ", path, d.IsDir())
return nil
}Output
Last updated