O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter06/recipe05.
  2. Navigate to the directory.
  3. Create a directory named folder.
  4. Create the listdir.go file with the following content:
        package main        import (          "fmt"          "io/ioutil"          "os"          "path/filepath"        )        func main() {          fmt.Println("List by ReadDir")          listDirByReadDir(".")          fmt.Println()          fmt.Println("List by Walk")          listDirByWalk(".")        }        func listDirByWalk(path string) {          filepath.Walk(path, func(wPath string, info os.FileInfo,                                   err error) error {          // Walk the given dir          // without printing out.          if wPath == path {            return nil          }          // If given path is folder          // stop list recursively and print as folder.          if info.IsDir() {            fmt.Printf("[%s]\n", wPath)            return filepath.SkipDir          }          // Print file name if wPath ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required