Calculating directory size
One of the most common things to be done is to check the size of directories. How can we do it using all our knowledge in Go? We first need to create a function to calculate the size of a directory:
func calculateDirSize(path string) (int64, error) { var size int64 err := filepath.Walk(path, func(filePath string, fileInfo os.FileInfo, err error) error { if err != nil { return err } if !fileInfo.IsDir() { size += fileInfo.Size() } return nil }) if err != nil { return 0, err } return size, nil }
This function calculates the total size of all files within a given directory, including its subdirectories. Let’s understand how this function works:
func calculateDirSize...