package main import ( "fmt" "os" ) func main() { fileInfo, err := os.Stat("example.txt") if err != nil { fmt.Println(err) return } fmt.Println(fileInfo.Name()) }
package main import ( "fmt" "os" ) func main() { dirPath := "/path/to/directory" files, err := os.ReadDir(dirPath) if err != nil { fmt.Println(err) return } for _, file := range files { fmt.Println(file.Name()) } }In this example, the os.ReadDir method is used to read the contents of a directory, and then the FileInfo.Name method is used to print the name of each file in the directory.