How do I walk directories recursively in Go?

In Go, you can walk through directories recursively using the `filepath` package, which provides a convenient way to navigate file paths. The `filepath.Walk` function allows you to traverse directories and process files as needed.

Keywords: Go, file handling, directory traversal, filepath, recursion
Description: This example demonstrates how to recursively walk through directories in Go using the filepath package. It processes each file and directory found during the traversal.

package main

import (
    "fmt"
    "log"
    "os"
    "path/filepath"
)

func main() {
    root := "." // Starting directory
    err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
        if err != nil {
            return err
        }
        if info.IsDir() {
            fmt.Println("Directory:", path)
        } else {
            fmt.Println("File:", path)
        }
        return nil
    })

    if err != nil {
        log.Fatal(err)
    }
}
    

Keywords: Go file handling directory traversal filepath recursion