diff --git a/helper/func.go b/helper/func.go index 3db7e19..60c0f15 100644 --- a/helper/func.go +++ b/helper/func.go @@ -8,6 +8,7 @@ import ( str "github.com/fthvgb1/wp-go/helper/strings" "net/url" "os" + "path/filepath" "reflect" "strconv" "strings" @@ -208,3 +209,34 @@ func AsError[T any](err error) (T, bool) { ok := errors.As(err, &v) return v, ok } + +func IsDirExistAndMkdir(dir string, perm os.FileMode) error { + stat, err := os.Stat(dir) + if err != nil { + if !os.IsNotExist(err) { + return err + } else { + return os.MkdirAll(dir, perm) + } + } + if !stat.IsDir() { + return fmt.Errorf("%s is exist but not dir", dir) + } + return nil +} + +func ReadDir(dir string) ([]string, error) { + fii, err := os.ReadDir(dir) + if err != nil { + return nil, err + } + var files []string + for _, entry := range fii { + name := entry.Name() + if name == "." || name == ".." { + continue + } + files = append(files, filepath.Join(dir, name)) + } + return files, nil +} diff --git a/helper/slice/sort.go b/helper/slice/sort.go index 646a877..8473889 100644 --- a/helper/slice/sort.go +++ b/helper/slice/sort.go @@ -27,7 +27,7 @@ func (r anyArr[T]) Less(i, j int) bool { return r.fn(r.data[i], r.data[j]) } -// Sort fn i>j 为降序 desc,反之为升序 asc +// Sort fn i>j desc ↓,i j + } + return i < j + }, + } + sort.Sort(slice) +}