add some func
This commit is contained in:
parent
7cbfdf8601
commit
63f769796f
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 asc ↑
|
||||
func Sort[T any](arr []T, fn func(i, j T) bool) {
|
||||
slice := anyArr[T]{
|
||||
data: arr,
|
||||
|
@ -84,3 +84,16 @@ func SimpleSort[T any, O constraints.Ordered](a []T, order string, fn func(t T)
|
|||
}
|
||||
sort.Sort(slice)
|
||||
}
|
||||
|
||||
func SimpleSorts[T constraints.Ordered](a []T, order string) {
|
||||
slice := anyArr[T]{
|
||||
data: a,
|
||||
fn: func(i, j T) bool {
|
||||
if order == DESC {
|
||||
return i > j
|
||||
}
|
||||
return i < j
|
||||
},
|
||||
}
|
||||
sort.Sort(slice)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user