# Maputil

maputil 包包括一些操作 map 的函数。

# 源码:

# 用法:

import (
    "github.com/duke-git/lancet/v2/maputil"
)

# API 文档:

# ForEach

对map中的每对key和value执行iteratee函数

函数签名:

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

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/maputil"
)

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

	var sum int

	maputil.ForEach(m, func(_ string, value int) {
		sum += value
	})
	fmt.Println(sum) // 10
}

# Filter

迭代map中的每对key和value, 返回符合predicate函数的key, value

函数签名:

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

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/maputil"
)

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

	maputil.Filter(m, func(_ string, value int) {
		sum += value
	})
	res := maputil.Filter(m, isEven)
	fmt.Println(res) // map[string]int{"b": 2, "d": 4,}
}

# Intersect

多个map的交集操作

函数签名:

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

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/maputil"
)

func main() {
	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,
	}

	fmt.Println(maputil.Intersect(m1)) // map[string]int{"a": 1, "b": 2, "c": 3}

	fmt.Println(maputil.Intersect(m1, m2)) // map[string]int{"a": 1, "b": 2}

	fmt.Println(maputil.Intersect(m1, m2, m3)) // map[string]int{"a": 1}
}

# Keys

返回map中所有key的切片

函数签名:

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

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/maputil"
)

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

	keys := maputil.Keys(m)
	sort.Ints(keys)
	fmt.Println(keys) // []int{1, 2, 3, 4, 5}
}

# Merge

合并多个maps, 相同的key会被后来的key覆盖

函数签名:

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

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/maputil"
)

func main() {
	m1 := map[int]string{
		1: "a",
		2: "b",
	}
	m2 := map[int]string{
		1: "1",
		3: "2",
	}
	fmt.Println(maputil.Merge(m1, m2)) // map[int]string{1:"1", 2:"b", 3:"2",}
}

# Minus

返回一个map,其中的key存在于mapA,不存在于mapB.

函数签名:

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

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/maputil"
)

func main() {
	m1 := map[string]int{
		"a": 1,
		"b": 2,
		"c": 3,
	}

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

	fmt.Println(maputil.Minus(m1, m2)) //map[string]int{"c": 3}
}

# Values

返回map中所有value的切片

函数签名:

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

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/maputil"
)

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

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

	fmt.Println(values) // []string{"a", "a", "b", "c", "d"}
}

# IsDisjoint

验证两个map是否具有不同的key

函数签名:

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

例子:

package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/maputil"
)

func main() {
	m1 := map[int]string{
		1: "a",
		2: "a",
		3: "b",
		4: "c",
		5: "d",
	}

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

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

	ok := maputil.IsDisjoint(m2, m1)
	fmt.Println(ok) // false

	ok = maputil.IsDisjoint(m2, m3)
	fmt.Println(ok) // true
}
最后更新时间: 2022/9/29 下午1:53:14