Skip to content

Instantly share code, notes, and snippets.

@rnemeth90
Created June 24, 2023 09:57
Show Gist options
  • Select an option

  • Save rnemeth90/4ac3a8d089d80da20447b3e4d7374dd7 to your computer and use it in GitHub Desktop.

Select an option

Save rnemeth90/4ac3a8d089d80da20447b3e4d7374dd7 to your computer and use it in GitHub Desktop.
Go filepath.walk example
package main
import (
"fmt"
"os"
"path"
"path/filepath"
)
func main() {
gopath := os.Getenv("GOPATH")
fmt.Printf("[%s/bin]\n", gopath)
list := getShellScript(gopath)
for i, p := range list {
fmt.Printf("[%d:%s===%s]\n", i, path.Dir(p), path.Base(p))
}
}
func getShellScript(rootpath string) []string {
list := make([]string, 0, 10)
err := filepath.Walk(rootpath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if filepath.Ext(path) == ".sh" {
list = append(list, path)
}
return nil
})
if err != nil {
fmt.Printf("walk error [%v]\n", err)
}
return list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment