package main import ( "fmt" "os" ) func main() { fileInfo, err := os.Stat("test.txt") if err != nil { fmt.Println("File does not exist") return } fmt.Println("File exists and its size is", fileInfo.Size(), "bytes") }
package main import ( "fmt" "os" ) func main() { err := os.Rename("oldfile.txt", "newfile.txt") if err != nil { fmt.Println("Rename failed:", err) return } fmt.Println("File renamed successfully.") }
package main import ( "fmt" "os" ) func main() { err := os.Mkdir("newdir", 0777) if err != nil { fmt.Println("Create directory failed:", err) return } fmt.Println("Directory created successfully.") }
package main import ( "fmt" "os" "path/filepath" ) func main() { root := "/Users/john/Documents" err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error { if err != nil { return err } if !info.IsDir() { fmt.Println(path) } return nil }) if err != nil { fmt.Println("Error:", err) } }Package: os, filepath