collection

package
v0.0.0-...-f56b5c8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2024 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForEach

func ForEach[T any | []any](array []T, f func(index int, value T))

func Map2Slice

func Map2Slice[K MapKeys, V any](m map[K]V) (keys []K, values []V)

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 SortKeys

func SortKeys[K MapKeys, V any, T interface{ []K }](m map[K]V) T

SortKeys

From: https://studygolang.com/articles/10530

func SortSlice

func SortSlice[T SortableTypes](slice []T) (sorted []T)

Types

type MapKeys

type MapKeys interface {
	int | int8 | int32 | int64 | string | float32 | float64 | uint | uint8 | uint16 | uint32 | uint64
}

type SortableTypes

type SortableTypes interface {
	int | int8 | int32 | int64 | string | float32 | float64 | uint | uint8 | uint16 | uint32 | uint64 | any
}

SortableTypes : MapKeys 的超集

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL