代码拉取完成,页面将自动刷新
package common
import (
"errors"
"reflect"
"strings"
"sync"
)
// IncludeElement 包含 找src在all
func IncludeElement[T EleType](all []T, src ...T) []T {
if len(src) == 0 {
return all
}
if len(all) == 0 {
return []T{}
}
var rest []T
for i := range src {
for j := range all {
if all[j] == src[i] {
rest = append(rest, src[i])
break
}
}
}
return rest
}
// ExcludeElement 排除 找src不存在all
func ExcludeElement[T EleType](all []T, src ...T) []T {
if len(src) == 0 {
return []T{}
}
if len(all) == 0 {
return src
}
var rest []T
for i := range src {
found := false
for j := range all {
if all[j] == src[i] {
found = true
break
}
}
if !found {
rest = append(rest, src[i])
}
}
return rest
}
// RemoveDuplicateElement 除重
func RemoveDuplicateElement[T EleType](values []T) []T {
result := make([]T, 0, len(values))
temp := map[T]struct{}{}
for i := range values {
if _, ok := temp[values[i]]; !ok {
temp[values[i]] = struct{}{}
result = append(result, values[i])
}
}
return result
}
// RemoveElement 移除某元素
func RemoveElement[T EleType](all []T, src T) []T {
return FilterElement(all, func(v T) bool {
return src != v
})
}
type FilterFunc[T EleType] func(v T) bool
// FilterElement 过滤某元素
func FilterElement[T EleType](all []T, cb FilterFunc[T]) []T {
var rest []T
for i := range all {
if cb(all[i]) {
rest = append(rest, all[i])
}
}
return rest
}
func FindElement[T EleType](all []T, src T) bool {
for i := range all {
if src == all[i] {
return true
}
}
return false
}
type EleType interface {
string | bool | int8 | int16 | int | int32 | int64 | uint8 | uint16 | uint | uint32 | uint64 | float32 | float64
}
// Defined checks if variable is defined in a struct
func Defined(data interface{}, field string) bool {
t := reflect.Indirect(reflect.ValueOf(data)).Type()
if t.Kind() != reflect.Struct {
return false
}
_, b := t.FieldByName(field)
return b
}
// Dict allows to send more than one variable into a template
func Dict(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
}
func IsNil(o interface{}) bool {
if o == nil {
return true
}
val := reflect.ValueOf(o)
switch val.Kind() {
case reflect.Pointer, reflect.UnsafePointer:
fallthrough
case reflect.Chan, reflect.Func, reflect.Map:
fallthrough
case reflect.Interface, reflect.Slice:
return val.IsNil()
}
return false
}
// IsMap checks if some variable is a map
func IsMap(o interface{}) bool {
if o == nil {
return false
}
return reflect.ValueOf(o).Kind() == reflect.Map
}
// IsSlice checks if some variable is a slice
func IsSlice(o interface{}) bool {
if o == nil {
return false
}
return reflect.ValueOf(o).Kind() == reflect.Slice
}
func StructVal(s interface{}) reflect.Value {
v := reflect.ValueOf(s)
// if pointer get the underlying element≤
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
panic("not struct")
}
return v
}
func StructFields(value reflect.Value, tagName string) []reflect.StructField {
t := value.Type()
var f []reflect.StructField
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
// we can't access the value of unexported fields
if field.PkgPath != "" {
continue
}
// don't check if it's omitted
if tag := field.Tag.Get(tagName); tag == "-" {
continue
}
f = append(f, field)
}
return f
}
// tagOptions contains a slice of tag options
type tagOptions []string
// Has returns true if the given option is available in tagOptions
func (t tagOptions) Has(opt string) bool {
for _, tagOpt := range t {
if tagOpt == opt {
return true
}
}
return false
}
func ParseTag(tag string) (string, tagOptions) {
// tag is one of followings:
// ""
// "name"
// "name,opt"
// "name,opt,opt2"
// ",opt"
res := strings.Split(tag, ",")
return res[0], res[1:]
}
type RunningCheck struct {
rm sync.Mutex
fRun bool
}
func (rc *RunningCheck) IsRunning() bool {
rc.rm.Lock()
defer rc.rm.Unlock()
return rc.fRun
}
func (rc *RunningCheck) TryGoRunning(fn func()) {
rc.rm.Lock()
if rc.fRun {
rc.rm.Unlock()
return
}
rc.fRun = true
rc.rm.Unlock()
go func() {
defer Recover(func() {
rc.rm.Lock()
rc.fRun = false
rc.rm.Unlock()
})
fn()
}()
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。