mapx

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2023 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Additional functions for dealing with maps.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assign

func Assign[M ~map[K]V, K comparable, V any](target M, sources ...M) M

Copies one or more items from the source maps to the target map. The later key-value pairs override the existing ones or the ones before them.

This function mutates the target map and returns it.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m1 := mapx.Assign(map[string]string{}, map[string]string{
		"foo": "Hello",
	})
	m2 := mapx.Assign(map[string]string{}, m1, map[string]string{
		"bar": "World",
	})

	fmt.Println(m1)
	fmt.Println(m2)
}
Output:

map[foo:Hello]
map[bar:World foo:Hello]

func ForEach added in v0.3.0

func ForEach[M ~map[K]V, K comparable, V any](m M, fn func(value V, key K))

Executes a provided function once for each key-value pair.

This function adds a closure context around each item looped, may be useful for preventing variable pollution.

Example (Int)
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m := map[int]string{
		0: "Hello",
		1: "World",
	}

	mapx.ForEach(m, func(value string, key int) {
		fmt.Println(key, "=>", value)
	})
}
Output:

0 => Hello
1 => World
Example (String)
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m := map[string]string{
		"foo": "Hello",
		"bar": "World",
	}

	mapx.ForEach(m, func(value, key string) {
		fmt.Println(key, "=>", value)
	})
}
Output:

foo => Hello
bar => World

func Keys

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

Returns the keys of the given map.

Keys are sorted in ascending order if they are strings or integers.

Example (Int)
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m := map[int]string{
		0: "Hello",
		1: "World",
	}
	keys := mapx.Keys(m)

	fmt.Println(keys)
}
Output:

[0 1]
Example (String)
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m := map[string]string{
		"foo": "Hello",
		"bar": "World",
	}
	keys := mapx.Keys(m)

	fmt.Println(keys)
}
Output:

[bar foo]

func Omit

func Omit[M ~map[K]V, K comparable, V any](original M, keys []K) M

Creates a new map based on the original map but without the specified keys.

Example (Int)
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m1 := map[int]string{
		0: "Hello",
		1: "World",
	}
	m2 := mapx.Omit(m1, []int{1})

	fmt.Println(m2)
}
Output:

map[0:Hello]
Example (String)
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m1 := map[string]string{
		"foo": "Hello",
		"bar": "World",
	}
	m2 := mapx.Omit(m1, []string{"bar"})

	fmt.Println(m2)
}
Output:

map[foo:Hello]

func Patch

func Patch[M ~map[K]V, K comparable, V any](target M, sources ...M) M

Copies the key-value pairs that are presented in the source maps but are missing in the target map into the target map, later pairs are skipped if the same key already exists.

This function mutates the target map and returns it.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m1 := mapx.Patch(map[string]string{}, map[string]string{
		"foo": "Hello",
	})
	m2 := mapx.Patch(map[string]string{}, m1, map[string]string{
		"foo": "Hi",
		"bar": "World",
	})

	fmt.Println(m1)
	fmt.Println(m2)
}
Output:

map[foo:Hello]
map[bar:World foo:Hello]

func Pick

func Pick[M ~map[K]V, K comparable, V any](original M, keys []K) M

Creates a new map based on the original map but only contains the specified keys.

Example (Int)
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m1 := map[int]string{
		0: "Hello",
		1: "World",
	}
	m2 := mapx.Pick(m1, []int{0})

	fmt.Println(m2)
}
Output:

map[0:Hello]
Example (String)
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m1 := map[string]string{
		"foo": "Hello",
		"bar": "World",
	}
	m2 := mapx.Pick(m1, []string{"foo"})

	fmt.Println(m2)
}
Output:

map[foo:Hello]

func Values

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

Returns the values of the given map.

Values are ordered according to the keys' order returned by `Keys()`.

Example
package main

import (
	"fmt"

	"github.com/ayonli/goext/mapx"
)

func main() {
	m := map[string]string{
		"foo": "Hello",
		"bar": "World",
	}
	values := mapx.Values(m)

	fmt.Println(values)
}
Output:

[World Hello]

Types

This section is empty.

Jump to

Keyboard shortcuts

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