Standard library / path/filepath

path/filepath.SkipDir

Callback sentinel to skip a directory; when returned for a file, skips remaining entries in its parent. Example uses a disposable temporary workspace. Expected stdout: '.\na.txt\n'.

Formal Syntax & Structure

go syntaxgo
var SkipDir error

Working Example

Example Snippetgo
package main
import (  "fmt"  "io/fs"  "os"  "path/filepath")
func check(e error) {  if e != nil {    panic(e)  }}func seed() { check(os.WriteFile("a.txt", []byte("abc"), 0600)) }func workspace() func() {  root, e := os.MkdirTemp("", "bytemind-go-")  check(e)  old, e := os.Getwd()  check(e)  check(os.Chdir(root))  return func() { check(os.Chdir(old)); check(os.RemoveAll(root)) }}func main() {  defer workspace()()  seed()  check(os.Mkdir("sub", 0700))  check(os.WriteFile("sub/b", nil, 0600))  check(filepath.WalkDir(".", func(p string, d fs.DirEntry, e error) error {    if e != nil {      return e    }    if p == "sub" {      return filepath.SkipDir    }    fmt.Println(p)    return nil  }))}
Go Reference