arrays

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: Apache-2.0 Imports: 0 Imported by: 0

Documentation

Index

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 Concat

func Concat(array []string, arrays ...[]string) []string

func DifferenceSet

func DifferenceSet[T comparable](arr1, arr2 []T) []T

func Distinct

func Distinct(array []string) []string

func GetFieldArrFromStruct

func GetFieldArrFromStruct[Struct any, Filed any](s []Struct, getField func(Struct) Filed) []Filed

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 IsContain

func IsContain(items []string, item string) bool

func Paging

func Paging(pageNo, pageSize, length uint64) (int64, int64)

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.

Jump to

Keyboard shortcuts

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