maputil

package
v2.3.4 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 10 Imported by: 21

Documentation

Overview

Package maputil includes some functions to manipulate map.

Package maputil includes some functions to manipulate map.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V

Filter iterates over map, return a new map contains all key and value pairs pass the predicate function. Play: https://go.dev/play/p/fSvF3wxuNG7

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
}

isEven := func(_ string, value int) bool {
	return value%2 == 0
}

result := Filter(m, isEven)

fmt.Println(result)
Output:

map[b:2 d:4]

func FilterByKeys added in v2.1.16

func FilterByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V

FilterByKeys iterates over map, return a new map whose keys are all given keys. Play: https://go.dev/play/p/7ov6BJHbVqh

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
}

result := FilterByKeys(m, []string{"a", "b"})

fmt.Println(result)
Output:

map[a:1 b:2]

func FilterByValues added in v2.1.16

func FilterByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V

FilterByValues iterates over map, return a new map whose values are all given values. Play: https://go.dev/play/p/P3-9MdcXegR

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
}

result := FilterByValues(m, []int{3, 4})

fmt.Println(result)
Output:

map[c:3 d:4]

func ForEach

func ForEach[K comparable, V any](m map[K]V, iteratee func(key K, value V))

ForEach executes iteratee funcation for every key and value pair in map. Play: https://go.dev/play/p/OaThj6iNVXK

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
}

var sum int
ForEach(m, func(_ string, value int) {
	sum += value
})

fmt.Println(sum)
Output:

10

func FromEntries added in v2.1.16

func FromEntries[K comparable, V any](entries []Entry[K, V]) map[K]V

FromEntries creates a map based on a slice of key/value pairs Play: https://go.dev/play/p/fTdu4sCNjQO

Example
result := FromEntries([]Entry[string, int]{
	{Key: "a", Value: 1},
	{Key: "b", Value: 2},
	{Key: "c", Value: 3},
})

fmt.Println(result)
Output:

map[a:1 b:2 c:3]

func GetOrDefault added in v2.3.3

func GetOrDefault[K comparable, V any](m map[K]V, key K, defaultValue V) V

GetOrDefault returns the value of the given key or a default value if the key is not present. Play: https://go.dev/play/p/99QjSYSBdiM

func GetOrSet added in v2.3.2

func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V

GetOrSet returns value of the given key or set the given value value if not present. Play: https://go.dev/play/p/IVQwO1OkEJC

Example
m := map[int]string{
	1: "a",
}

result1 := GetOrSet(m, 1, "1")
result2 := GetOrSet(m, 2, "b")

fmt.Println(result1)
fmt.Println(result2)
Output:

a
b

func HasKey added in v2.2.4

func HasKey[K comparable, V any](m map[K]V, key K) bool

HasKey checks if map has key or not. This function is used to replace the following boilerplate code: _, haskey := amap["baz"];

if haskey {
   fmt.Println("map has key baz")
}

Play: https://go.dev/play/p/isZZHOsDhFc

Example
m := map[string]int{
	"a": 1,
	"b": 2,
}

result1 := HasKey(m, "a")
result2 := HasKey(m, "c")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func Intersect

func Intersect[K comparable, V any](maps ...map[K]V) map[K]V

Intersect iterates over maps, return a new map of key and value pairs in all given maps. Play: https://go.dev/play/p/Zld0oj3sjcC

Example
m1 := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
}

m2 := map[string]int{
	"a": 1,
	"b": 2,
	"c": 6,
	"d": 7,
}

m3 := map[string]int{
	"a": 1,
	"b": 9,
	"e": 9,
}

result1 := Intersect(m1)
result2 := Intersect(m1, m2)
result3 := Intersect(m1, m2, m3)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
Output:

map[a:1 b:2 c:3]
map[a:1 b:2]
map[a:1]

func IsDisjoint added in v2.1.3

func IsDisjoint[K comparable, V any](mapA, mapB map[K]V) bool

IsDisjoint two map are disjoint if they have no keys in common. Play: https://go.dev/play/p/N9qgYg_Ho6f

Example
m1 := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
}

m2 := map[string]int{
	"d": 22,
}

m3 := map[string]int{
	"a": 22,
}

result1 := IsDisjoint(m1, m2)
result2 := IsDisjoint(m1, m3)

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys returns a slice of the map's keys. Play: https://go.dev/play/p/xNB5bTb97Wd

Example
m := map[int]string{
	1: "a",
	2: "a",
	3: "b",
	4: "c",
	5: "d",
}

keys := Keys(m)
sort.Ints(keys)

fmt.Println(keys)
Output:

[1 2 3 4 5]

func KeysBy added in v2.1.16

func KeysBy[K comparable, V any, T any](m map[K]V, mapper func(item K) T) []T

KeysBy creates a slice whose element is the result of function mapper invoked by every map's key. Play: https://go.dev/play/p/hI371iB8Up8

Example
m := map[int]string{
	1: "a",
	2: "a",
	3: "b",
}

keys := KeysBy(m, func(n int) int {
	return n + 1
})

sort.Ints(keys)

fmt.Println(keys)
Output:

[2 3 4]

func MapKeys added in v2.1.16

func MapKeys[K comparable, V any, T comparable](m map[K]V, iteratee func(key K, value V) T) map[T]V

MapKeys transforms a map to other type map by manipulating it's keys. Play: https://go.dev/play/p/8scDxWeBDKd

Example
m := map[int]string{
	1: "a",
	2: "b",
	3: "c",
}

result := MapKeys(m, func(k int, _ string) string {
	return strconv.Itoa(k)
})

fmt.Println(result)
Output:

map[1:a 2:b 3:c]

func MapTo added in v2.1.19

func MapTo(src any, dst any) error

MapTo try to map any interface to struct or base type

Eg:
	v := map[string]interface{}{
		"service":map[string]interface{}{
			"ip":"127.0.0.1",
			"port":1234,
		},
		version:"v1.0.01"
	}

	type Target struct {
		Service struct {
			Ip string `json:"ip"`
			Port int `json:"port"`
		} `json:"service"`
		Ver string `json:"version"`
	}

	var dist Target
	if err := maputil.MapTo(v,&dist); err != nil {
		log.Println(err)
		return
	}

	log.Println(dist)

Play: https://go.dev/play/p/4K7KBEPgS5M

Example
type (
	Person struct {
		Name  string  `json:"name"`
		Age   int     `json:"age"`
		Phone string  `json:"phone"`
		Addr  Address `json:"address"`
	}

	Address struct {
		Street string `json:"street"`
		Number int    `json:"number"`
	}
)

personInfo := map[string]interface{}{
	"name":  "Nothin",
	"age":   28,
	"phone": "123456789",
	"address": map[string]interface{}{
		"street": "test",
		"number": 1,
	},
}

var p Person
err := MapTo(personInfo, &p)

fmt.Println(err)
fmt.Println(p)
Output:

<nil>
{Nothin 28 123456789 {test 1}}

func MapToStruct added in v2.3.0

func MapToStruct(m map[string]any, structObj any) error

MapToStruct converts map to struct Play: https://go.dev/play/p/7wYyVfX38Dp

Example
personReqMap := map[string]any{
	"name":     "Nothin",
	"max_age":  35,
	"page":     1,
	"pageSize": 10,
}

type PersonReq struct {
	Name     string `json:"name"`
	MaxAge   int    `json:"max_age"`
	Page     int    `json:"page"`
	PageSize int    `json:"pageSize"`
}
var personReq PersonReq
_ = MapToStruct(personReqMap, &personReq)
fmt.Println(personReq)
Output:

{Nothin 35 1 10}

func MapValues added in v2.1.16

func MapValues[K comparable, V any, T any](m map[K]V, iteratee func(key K, value V) T) map[K]T

MapValues transforms a map to other type map by manipulating it's values. Play: https://go.dev/play/p/g92aY3fc7Iw

Example
m := map[int]string{
	1: "a",
	2: "b",
	3: "c",
}

result := MapValues(m, func(k int, v string) string {
	return v + strconv.Itoa(k)
})

fmt.Println(result)
Output:

map[1:a1 2:b2 3:c3]

func Merge

func Merge[K comparable, V any](maps ...map[K]V) map[K]V

Merge maps, next key will overwrite previous key. Play: https://go.dev/play/p/H95LENF1uB-

Example
m1 := map[int]string{
	1: "a",
	2: "b",
}
m2 := map[int]string{
	1: "c",
	3: "d",
}

result := Merge(m1, m2)

fmt.Println(result)
Output:

map[1:c 2:b 3:d]

func Minus

func Minus[K comparable, V any](mapA, mapB map[K]V) map[K]V

Minus creates a map of whose key in mapA but not in mapB. Play: https://go.dev/play/p/3u5U9K7YZ9m

Example
m1 := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
}

m2 := map[string]int{
	"a": 11,
	"b": 22,
	"d": 33,
}

result := Minus(m1, m2)

fmt.Println(result)
Output:

map[c:3]

func OmitBy added in v2.1.16

func OmitBy[K comparable, V any](m map[K]V, predicate func(key K, value V) bool) map[K]V

OmitBy is the opposite of Filter, removes all the map elements for which the predicate function returns true. Play: https://go.dev/play/p/YJM4Hj5hNwm

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
}
isEven := func(_ string, value int) bool {
	return value%2 == 0
}

result := OmitBy(m, isEven)

fmt.Println(result)
Output:

map[a:1 c:3 e:5]

func OmitByKeys added in v2.1.16

func OmitByKeys[K comparable, V any](m map[K]V, keys []K) map[K]V

OmitByKeys the opposite of FilterByKeys, extracts all the map elements which keys are not omitted. Play: https://go.dev/play/p/jXGrWDBfSRp

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
}

result := OmitByKeys(m, []string{"a", "b"})

fmt.Println(result)
Output:

map[c:3 d:4 e:5]

func OmitByValues added in v2.1.16

func OmitByValues[K comparable, V comparable](m map[K]V, values []V) map[K]V

OmitByValues the opposite of FilterByValues. remov all elements whose value are in the give slice. Play: https://go.dev/play/p/XB7Y10uw20_U

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
	"d": 4,
	"e": 5,
}

result := OmitByValues(m, []int{4, 5})

fmt.Println(result)
Output:

map[a:1 b:2 c:3]

func SortByKey added in v2.3.3

func SortByKey[K constraints.Ordered, V any](m map[K]V, less func(a, b K) bool) (sortedKeysMap map[K]V)

SortByKey sorts the map by its keys and returns a new map with sorted keys. Play: https://go.dev/play/p/PVdmBSnm6P_W

Example
m := map[int]string{
	3: "c",
	1: "a",
	4: "d",
	2: "b",
}

result := SortByKey(m, func(a, b int) bool {
	return a < b
})

fmt.Println(result)
Output:

map[1:a 2:b 3:c 4:d]

func ToSortedSlicesDefault added in v2.3.1

func ToSortedSlicesDefault[K constraints.Ordered, V any](m map[K]V) ([]K, []V)

ToSortedSlicesDefault converts a map to two slices sorted by key: one for the keys and another for the values. Play: https://go.dev/play/p/43gEM2po-qy

Example
m := map[int]string{
	1: "a",
	3: "c",
	2: "b",
}

keys, values := ToSortedSlicesDefault(m)

fmt.Println(keys)
fmt.Println(values)
Output:

[1 2 3]
[a b c]

func ToSortedSlicesWithComparator added in v2.3.1

func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator func(a, b K) bool) ([]K, []V)

ToSortedSlicesWithComparator converts a map to two slices sorted by key and using a custom comparison function: one for the keys and another for the values. Play: https://go.dev/play/p/0nlPo6YLdt3

Example
m1 := map[time.Time]string{
	time.Date(2024, 3, 31, 0, 0, 0, 0, time.UTC): "today",
	time.Date(2024, 3, 30, 0, 0, 0, 0, time.UTC): "yesterday",
	time.Date(2024, 4, 1, 0, 0, 0, 0, time.UTC):  "tomorrow",
}

keys1, values1 := ToSortedSlicesWithComparator(m1, func(a, b time.Time) bool {
	return a.Before(b)
})

m2 := map[int]string{
	1: "a",
	3: "c",
	2: "b",
}
keys2, values2 := ToSortedSlicesWithComparator(m2, func(a, b int) bool {
	return a > b
})

fmt.Println(keys1)
fmt.Println(values1)

fmt.Println(keys2)
fmt.Println(values2)
Output:

[2024-03-30 00:00:00 +0000 UTC 2024-03-31 00:00:00 +0000 UTC 2024-04-01 00:00:00 +0000 UTC]
[yesterday today tomorrow]
[3 2 1]
[c b a]

func Transform added in v2.1.16

func Transform[K1 comparable, V1 any, K2 comparable, V2 any](m map[K1]V1, iteratee func(key K1, value V1) (K2, V2)) map[K2]V2

Transform a map to another type map. Play: https://go.dev/play/p/P6ovfToM3zj

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
}

result := Transform(m, func(k string, v int) (string, string) {
	return k, strconv.Itoa(v)
})

fmt.Println(result)
Output:

map[a:1 b:2 c:3]

func Values

func Values[K comparable, V any](m map[K]V) []V

Values returns a slice of the map's values. Play: https://go.dev/play/p/CBKdUc5FTW6

Example
m := map[int]string{
	1: "a",
	2: "a",
	3: "b",
	4: "c",
	5: "d",
}

values := Values(m)
sort.Strings(values)

fmt.Println(values)
Output:

[a a b c d]

func ValuesBy added in v2.1.16

func ValuesBy[K comparable, V any, T any](m map[K]V, mapper func(item V) T) []T

ValuesBy creates a slice whose element is the result of function mapper invoked by every map's value. Play: https://go.dev/play/p/sg9-oRidh8f

Example
m := map[int]string{
	1: "a",
	2: "b",
	3: "c",
}
values := ValuesBy(m, func(v string) string {
	switch v {
	case "a":
		return "a-1"
	case "b":
		return "b-2"
	case "c":
		return "c-3"
	default:
		return ""
	}
})

sort.Strings(values)

fmt.Println(values)
Output:

[a-1 b-2 c-3]

Types

type ConcurrentMap added in v2.2.4

type ConcurrentMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

ConcurrentMap is like map, but is safe for concurrent use by multiple goroutines.

func NewConcurrentMap added in v2.2.4

func NewConcurrentMap[K comparable, V any](shardCount int) *ConcurrentMap[K, V]

NewConcurrentMap create a ConcurrentMap with specific shard count. Play: https://go.dev/play/p/3PenTPETJT0

func (*ConcurrentMap[K, V]) Delete added in v2.2.4

func (cm *ConcurrentMap[K, V]) Delete(key K)

Delete the value for a key. Play: https://go.dev/play/p/uTIJZYhpVMS

func (*ConcurrentMap[K, V]) Get added in v2.2.4

func (cm *ConcurrentMap[K, V]) Get(key K) (V, bool)

Get the value stored in the map for a key, or nil if no. Play: https://go.dev/play/p/3PenTPETJT0

func (*ConcurrentMap[K, V]) GetAndDelete added in v2.2.4

func (cm *ConcurrentMap[K, V]) GetAndDelete(key K) (actual V, ok bool)

GetAndDelete returns the existing value for the key if present and then delete the value for the key. Otherwise, do nothing, just return false Play: https://go.dev/play/p/ZyxeIXSZUiM

func (*ConcurrentMap[K, V]) GetOrSet added in v2.2.4

func (cm *ConcurrentMap[K, V]) GetOrSet(key K, value V) (actual V, ok bool)

GetOrSet returns the existing value for the key if present. Otherwise, it sets and returns the given value. Play: https://go.dev/play/p/aDcDApOK01a

func (*ConcurrentMap[K, V]) Has added in v2.2.4

func (cm *ConcurrentMap[K, V]) Has(key K) bool

Has checks if map has the value for a key. Play: https://go.dev/play/p/C8L4ul9TVwf

func (*ConcurrentMap[K, V]) Range added in v2.2.4

func (cm *ConcurrentMap[K, V]) Range(iterator func(key K, value V) bool)

Range calls iterator sequentially for each key and value present in each of the shards in the map. If iterator returns false, range stops the iteration. Play: https://go.dev/play/p/iqcy7P8P0Pr

func (*ConcurrentMap[K, V]) Set added in v2.2.4

func (cm *ConcurrentMap[K, V]) Set(key K, value V)

Set the value for a key. Play: https://go.dev/play/p/3PenTPETJT0

type Entry added in v2.1.16

type Entry[K comparable, V any] struct {
	Key   K
	Value V
}

Entry is a key/value pairs.

func Entries added in v2.1.16

func Entries[K comparable, V any](m map[K]V) []Entry[K, V]

Entries transforms a map into array of key/value pairs. Play: https://go.dev/play/p/Ltb11LNcElY

Example
m := map[string]int{
	"a": 1,
	"b": 2,
	"c": 3,
}

result := Entries(m)

sort.Slice(result, func(i, j int) bool {
	return result[i].Value < result[j].Value
})

fmt.Println(result)
Output:

[{a 1} {b 2} {c 3}]

type OrderedMap added in v2.3.3

type OrderedMap[K comparable, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap is a map that maintains the order of keys.

func NewOrderedMap added in v2.3.3

func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]

NewOrderedMap creates a new OrderedMap.

func (*OrderedMap[K, V]) Back added in v2.3.3

func (om *OrderedMap[K, V]) Back() (struct {
	Key   K
	Value V
}, bool)

Back returns the last key-value pair. Play: https://go.dev/play/p/rQMjp1yQmpa

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

backElement, ok := om.Back()
fmt.Println(backElement)
fmt.Println(ok)
Output:

{c 3}
true

func (*OrderedMap[K, V]) Clear added in v2.3.3

func (om *OrderedMap[K, V]) Clear()

Clear clears the map. Play: https://go.dev/play/p/8LwoJyEfuFr

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

om.Clear()

fmt.Println(om.Keys())
Output:

[]

func (*OrderedMap[K, V]) Contains added in v2.3.3

func (om *OrderedMap[K, V]) Contains(key K) bool

Contains returns true if the given key exists. Play: https://go.dev/play/p/QuwqqnzwDNX

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

result1 := om.Contains("a")
result2 := om.Contains("d")

fmt.Println(result1)
fmt.Println(result2)
Output:

true
false

func (*OrderedMap[K, V]) Delete added in v2.3.3

func (om *OrderedMap[K, V]) Delete(key K)

Delete deletes the given key. Play: https://go.dev/play/p/5bIi4yaZ3K-

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

om.Delete("b")

fmt.Println(om.Keys())
Output:

[a c]

func (*OrderedMap[K, V]) Elements added in v2.3.3

func (om *OrderedMap[K, V]) Elements() []struct {
	Key   K
	Value V
}

Elements returns the key-value pairs in order. Play: https://go.dev/play/p/4BHG4kKz6bB

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

elements := om.Elements()

fmt.Println(elements)
Output:

[{a 1} {b 2} {c 3}]

func (*OrderedMap[K, V]) Front added in v2.3.3

func (om *OrderedMap[K, V]) Front() (struct {
	Key   K
	Value V
}, bool)

Front returns the first key-value pair. Play: https://go.dev/play/p/ty57XSimpoe

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

frontElement, ok := om.Front()
fmt.Println(frontElement)
fmt.Println(ok)
Output:

{a 1}
true

func (*OrderedMap[K, V]) Get added in v2.3.3

func (om *OrderedMap[K, V]) Get(key K) (V, bool)

Get returns the value for the given key. Play: https://go.dev/play/p/Y4ZJ_oOc1FU

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

val1, ok := om.Get("a")
fmt.Println(val1, ok)

val2, ok := om.Get("d")
fmt.Println(val2, ok)
Output:

1 true
0 false

func (*OrderedMap[K, V]) Iter added in v2.3.3

func (om *OrderedMap[K, V]) Iter() <-chan struct {
	Key   K
	Value V
}

Iter returns a channel that yields key-value pairs in order. Play: https://go.dev/play/p/tlq2tdvicPt

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

for elem := range om.Iter() {
	fmt.Println(elem)
}
Output:

{a 1}
{b 2}
{c 3}

func (*OrderedMap[K, V]) Keys added in v2.3.3

func (om *OrderedMap[K, V]) Keys() []K

Keys returns the keys in order. Play: https://go.dev/play/p/Vv_y9ExKclA

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

keys := om.Keys()

fmt.Println(keys)
Output:

[a b c]

func (*OrderedMap[K, V]) Len added in v2.3.3

func (om *OrderedMap[K, V]) Len() int

Len returns the number of key-value pairs. Play: https://go.dev/play/p/cLe6z2VX5N-

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

om.Len()

fmt.Println(om.Len())
Output:

3

func (*OrderedMap[K, V]) MarshalJSON added in v2.3.3

func (om *OrderedMap[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface. Play: https://go.dev/play/p/C-wAwydIAC7

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

b, _ := om.MarshalJSON()

fmt.Println(string(b))
Output:

{"a":1,"b":2,"c":3}

func (*OrderedMap[K, V]) Range added in v2.3.3

func (om *OrderedMap[K, V]) Range(iteratee func(key K, value V) bool)

Range calls the given function for each key-value pair. Play: https://go.dev/play/p/U-KpORhc7LZ

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

om.Range(func(key string, value int) bool {
	fmt.Println(key, value)
	return true
})
Output:

a 1
b 2
c 3

func (*OrderedMap[K, V]) ReverseIter added in v2.3.3

func (om *OrderedMap[K, V]) ReverseIter() <-chan struct {
	Key   K
	Value V
}

ReverseIter returns a channel that yields key-value pairs in reverse order. Play: https://go.dev/play/p/8Q0ssg6hZzO

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

for elem := range om.ReverseIter() {
	fmt.Println(elem)
}
Output:

{c 3}
{b 2}
{a 1}

func (*OrderedMap[K, V]) Set added in v2.3.3

func (om *OrderedMap[K, V]) Set(key K, value V)

Sets the given key-value pair. Play: https://go.dev/play/p/Y4ZJ_oOc1FU

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

val1, ok := om.Get("a")
fmt.Println(val1, ok)

val2, ok := om.Get("d")
fmt.Println(val2, ok)
Output:

1 true
0 false

func (*OrderedMap[K, V]) SortByKey added in v2.3.3

func (om *OrderedMap[K, V]) SortByKey(less func(a, b K) bool)

SortByValue sorts the map by key given less function. Play: https://go.dev/play/p/N7hjD_alZPq

Example
om := NewOrderedMap[int, string]()

om.Set(3, "c")
om.Set(1, "a")
om.Set(4, "d")
om.Set(2, "b")

om.SortByKey(func(a, b int) bool {
	return a < b
})

fmt.Println(om.Elements())
Output:

[{1 a} {2 b} {3 c} {4 d}]

func (*OrderedMap[K, V]) UnmarshalJSON added in v2.3.3

func (om *OrderedMap[K, V]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. Play: https://go.dev/play/p/8C3MvJ3-mut

Example
// om := NewOrderedMap[string, int]()

// data := []byte(`{"a":1,"b":2,"c":3}`)

// om.UnmarshalJSON(data)

// fmt.Println(om.Elements())
Output:

func (*OrderedMap[K, V]) Values added in v2.3.3

func (om *OrderedMap[K, V]) Values() []V

Values returns the values in order. Play: https://go.dev/play/p/TWj5n1-PUfx

Example
om := NewOrderedMap[string, int]()

om.Set("a", 1)
om.Set("b", 2)
om.Set("c", 3)

values := om.Values()

fmt.Println(values)
Output:

[1 2 3]

Jump to

Keyboard shortcuts

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