Documentation
¶
Index ¶
- func ArrayToMap[Key comparable](keys []Key) map[Key]struct{}
- func Concat(array []string, arrays ...[]string) []string
- func DifferenceSet[T comparable](arr1, arr2 []T) []T
- func Distinct(array []string) []string
- func GetFieldArrFromStruct[Struct any, Filed any](s []Struct, getField func(Struct) Filed) []Filed
- func IsArrayContained[T comparable](array []T, sub []T) (int, bool)
- func IsContain(items []string, item string) bool
- func Paging(pageNo, pageSize, length uint64) (int64, int64)
- func StructArrayToMap[T any, Key comparable, Value comparable](arr []T, getKV func(T) (Key, Value, bool)) map[Key]Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ArrayToMap ¶
func ArrayToMap[Key comparable](keys []Key) map[Key]struct{}
ArrayToMap convert the deduplicated array into map
Example ¶
package main
import (
"fmt"
"github.com/erda-project/erda/pkg/arrays"
)
func main() {
arr := []int{1, 2, 3, 4, 3}
result := arrays.ArrayToMap(arr)
fmt.Println(result)
}
Output: map[1:{} 2:{} 3:{} 4:{}]
func DifferenceSet ¶
func DifferenceSet[T comparable](arr1, arr2 []T) []T
func GetFieldArrFromStruct ¶
GetFieldArrFromStruct construct a elem array from the structs
Example ¶
package main
import (
"fmt"
"github.com/erda-project/erda/pkg/arrays"
)
func main() {
type itemStruct struct {
Field string
}
arr := []itemStruct{
{Field: "123"},
{Field: "5321"},
{Field: "45"},
}
result := arrays.GetFieldArrFromStruct(arr, func(item itemStruct) string {
return item.Field
})
fmt.Println(result)
}
Output: [123 5321 45]
func IsArrayContained ¶
func IsArrayContained[T comparable](array []T, sub []T) (int, bool)
IsArrayContained Check if the elements in the `sub` array are a subset of the `array` It returns (-1,true) if all elements in `sub` are present int `array` Otherwise it returns first elements index in `sub` which is not a subset of the `array` and false
func StructArrayToMap ¶
func StructArrayToMap[T any, Key comparable, Value comparable](arr []T, getKV func(T) (Key, Value, bool)) map[Key]Value
StructArrayToMap converts a struct array into a map after deduplication and you should offer a fn to get the key,value and ifSkip the kvs from the struct.
Example ¶
package main
import (
"fmt"
"github.com/erda-project/erda/pkg/arrays"
)
func main() {
type itemStruct struct {
Key string
Value string
Other interface{}
}
arr := []itemStruct{
{Key: "this is key1", Value: "this is value1", Other: ""},
{Key: "this is key2", Value: "this is value2", Other: ""},
{Key: "", Value: "", Other: ""},
}
array2Map := arrays.StructArrayToMap(arr, func(item itemStruct) (key string, value string, skip bool) {
if item.Key == "" {
return "", "", true
}
return item.Key, item.Value, false
})
fmt.Println(array2Map)
}
Output: map[this is key1:this is value1 this is key2:this is value2]
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.