github.com/searKing/golang/go@v1.2.74/path/filepath/match.go (about)

     1  // Copyright 2022 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package filepath
     6  
     7  import (
     8  	"path/filepath"
     9  )
    10  
    11  // GlobFunc returns the names of all files matching pattern satisfying f(c) or nil
    12  // if there is no matching file. The syntax of patterns is the same
    13  // as in Match. The pattern may describe hierarchical names such as
    14  // /usr/*/bin/ed (assuming the Separator is '/').
    15  //
    16  // Glob ignores file system errors such as I/O errors reading directories.
    17  // The only possible returned error is ErrBadPattern, when pattern
    18  // is malformed.
    19  func GlobFunc(pattern string, handler func(name string) bool) (matches []string, err error) {
    20  	matches, err = filepath.Glob(pattern)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	if handler == nil {
    25  		return matches, err
    26  	}
    27  
    28  	var a []string
    29  	for _, match := range matches {
    30  		if handler(match) {
    31  			a = append(a, match)
    32  		}
    33  	}
    34  	return a, nil
    35  }