Skip to content

Index

func Compose

go
func Compose(vals ...any) map[any]any

Compose makes a map with the given keys and values. Useful as a template function to pass multiple values to a template. Based on even and odd values.

Usage:

// Code
mapx.Compose("foo", 1, "bar", 2) // map[any]any{"foo": 1, "bar": 2}

func Delete

go
func Delete[T1 comparable, T2 any](m map[T1]T2, keys ...T1) map[T1]T2

Delete removes given keys from a given map.

Usage:

example := map[int]int{1: 2, 5: 6, 8: 10}
mapx.Delete(example, 5, 8) // map[int]int{1: 2}

func Keep

go
func Keep[T1 comparable, T2 any](m map[T1]T2, keys ...T1) map[T1]T2

Keep reverses logic of mapx.Delete. Removes keys from a given map, if key not found in the given keys.

Usage:

example := map[int]int{1: 2, 5: 6, 8: 10}
mapx.Keep(example, 5, 8) // map[int]int{5: 6, 8: 10}

func Keys

go
func Keys[T1 comparable, T2 any](m map[T1]T2) (keys []T1)

Keys extracts keys from a given map.

Usage:

example := map[int]int{1: 2, 5: 6, 8: 10}
mapx.Keys(example) // []int{1, 5, 8}

func Merge

go
func Merge[T1 comparable, T2 any](maps ...map[T1]T2) map[T1]T2

Merge merges given maps into single map. Each next map overrides previous one keys.

Usage:

res := mapx.Merge(  // map[string]int{"a": 1, "b": 3, "c": 4}
	map[string]int{"a": 1, "b": 2},
	map[string]int{"b": 3, "c": 4},
)

func Values

go
func Values[T1 comparable, T2 any](m map[T1]T2) (values []T2)

Values extracts values from a given map.

Usage:

example := map[int]int{1: 2, 5: 6, 8: 10}
mapx.Values(example) // []int{2, 6, 10}

Generated by gomarkdoc