package main import ( "fmt" "os" ) func main() { dir, err := os.Open(".") if err != nil { fmt.Println(err) return } defer dir.Close() files, err := dir.Readdir(0) if err != nil { fmt.Println(err) return } for _, file := range files { if file.Mode().IsRegular() { fmt.Println(file.Name()) } } }
package main import ( "fmt" "os" ) func main() { dir, err := os.Open(".") if err != nil { fmt.Println(err) return } defer dir.Close() files, err := dir.Readdir(0) if err != nil { fmt.Println(err) return } for _, file := range files { if file.IsDir() { fmt.Println(file.Name()) } } }This code will list all the directories in the current directory. In both examples, the os package is used to open a directory, read its contents using the File.Readdir method, and then work on the FileInfo values returned.