Documentation ¶
Index ¶
- func ForEach[T any | []any](array []T, f func(index int, value T))
- func Map2Slice[K MapKeys, V any](m map[K]V) (keys []K, values []V)
- func Slice2Map[K comparable, V any](slice []V, keySelector func(V) K) map[K]V
- func SliceDiff[T comparable](a, b []T) (missInA []T, missInB []T)
- func SortKeys[K MapKeys, V any, T interface{ ... }](m map[K]V) T
- func SortSlice[T SortableTypes](slice []T) (sorted []T)
- type MapKeys
- type SortableTypes
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Slice2Map ¶
func Slice2Map[K comparable, V any](slice []V, keySelector func(V) K) map[K]V
// 示例1:将整数切片转换为映射,键为整数值 intSlice := []int{1, 2, 3, 4}
intMap := Slice2Map(intSlice, func(item int) int { return item })
fmt.Println(intMap)
// 示例2:将字符串切片转换为映射,键为字符串本身 strSlice := []string{"apple", "banana", "cherry"}
strMap := Slice2Map(strSlice, func(item string) string { return item })
fmt.Println(strMap)
// 示例3:将结构体切片转换为映射,键为结构体的某个字段
type Person struct { ID int Name string } personSlice := []Person{ {ID: 1, Name: "Alice"}, {ID: 2, Name: "Bob"}, {ID: 3, Name: "Charlie"}, } personMap := Slice2Map(personSlice, func(item Person) int { return item.ID })
fmt.Println(personMap)
func SliceDiff ¶
func SliceDiff[T comparable](a, b []T) (missInA []T, missInB []T)
check the diff items between two slices
func SortSlice ¶
func SortSlice[T SortableTypes](slice []T) (sorted []T)
Types ¶
Click to show internal directories.
Click to hide internal directories.