Directories

This Go program demonstrates directory and file manipulation operations using the os and filepath packages. Let's go through the code with inline comments and explanations:

// 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

Listing subdir/parent
  child true
  file2 false
  file3 false
Listing subdir/parent/child
  file4 false
Visiting subdir
  subdir true
  subdir\file1 false
  subdir\parent true
  subdir\parent\child true
  subdir\parent\child\file4 false
  subdir\parent\file2 false
  subdir\parent\file3 false

Explanation:

  1. Creating and Removing Directories:

    • os.Mkdir("subdir", 0755) creates a directory named "subdir" with permissions 0755.

    • defer os.RemoveAll("subdir") defers the removal of the "subdir" directory until the end of the program.

  2. Creating Empty Files:

    • createEmptyFile function creates empty files using os.WriteFile.

  3. Nested Directories and Files:

    • os.MkdirAll("subdir/parent/child", 0755) creates nested directories.

    • createEmptyFile is used to create files within the nested directories.

  4. Reading Directory Contents:

    • os.ReadDir("subdir/parent") reads the contents of a directory.

    • entry.Name() and entry.IsDir() are used to print the name and whether it's a directory or not.

  5. Changing Working Directory:

    • os.Chdir("subdir/parent/child") changes the current working directory.

    • os.Chdir("../../..") changes it back to the root.

  6. Using filepath.WalkDir:

    • filepath.WalkDir("subdir", visit) recursively walks the directory tree and calls the visit function for each file or directory.

  7. visit Function:

    • visit function is called during the filepath.WalkDir traversal.

    • It prints the path and whether it's a directory or not.

This program showcases common file and directory manipulation operations in Go.

Last updated