代码拉取完成,页面将自动刷新
package common
import (
"errors"
"reflect"
)
func ElementsTo[T EleType](c []T) []interface{} {
var ret = make([]interface{}, len(c))
for i := range c {
ret[i] = c[i]
}
return ret
}
// 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
}
type ReverseType interface {
rune | bool | int8 | int16 | int64 | uint8 | uint16 | uint32 | uint64 | float32 | float64
}
func ReverseElement[T ReverseType](rs []T) []T {
for i, j := 0, len(rs)-1; i < j; i, j = i+1, j-1 {
rs[i], rs[j] = rs[j], rs[i]
}
return rs
}
// 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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。