当前位置: 首页 > news >正文

Go 1.21新增的 maps 包详解

maps 包提供了几个非常有用的用于操作 map 类型(任何类型的 map)的函数,本文接下来详细介绍下这几个函数。

maps.Clone

定义如下:

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

返回 m 的一个副本,因为新的键和值是使用赋值方式复制的,所以这是一个浅克隆。简单示例如下:

package mainimport ("fmt""maps"
)func main() {m := map[string]string{"foo": "bar"}m1 := maps.Clone(m)fmt.Println(m1) // map[foo:bar]
}

maps.Copy

定义如下:

func Copy[M1 ~map[K]V, M2 ~map[K]V, K comparable, V any](dst M1, src M2)

复制 src 中的所有键值对并添加到 dst 中。当 src 中的键已经在 dst 中存在时,dst 中的值将被 src 中被该键对应的值覆盖。简单示例如下:

package mainimport ("fmt""maps"
)func main() {m := map[string]string{"foo": "bar", "foo2": "bar2"}m1 := map[string]string{"foo": "bar2", "foo3": "bar3"}maps.Copy(m1, m)fmt.Println(m1) // map[foo:bar foo2:bar2 foo3:bar3]
}

maps.DeleteFunc

定义如下:

func DeleteFunc[M ~map[K]V, K comparable, V any](m M, del func(K, V) bool)

从 m 中删除经 del 函数计算后返回 true 的所有键值对。简单示例如下:

package mainimport ("fmt""maps"
)func main() {m := map[string]int{"one":   1,"two":   2,"three": 3,"four":  4,}maps.DeleteFunc(m, func(k string, v int) bool {return v%2 != 0 })fmt.Println(m)
}

删除所有值为奇数类型的键值对。

maps.Equal

定义如下:

func Equal[M1, M2 ~map[K]V, K, V comparable](m1 M1, m2 M2) bool

判断两个 map 是否包含相同的键值对。简单示例如下:

package mainimport ("fmt""maps"
)func main() {m := map[string]string{"foo": "bar", "foo2": "bar2"}m1 := map[string]string{"foo": "bar2", "foo3": "bar3"}m2 := map[string]string{"foo": "bar", "foo2": "bar2"}b := maps.Equal(m, m1)fmt.Println(b) // falseb = maps.Equal(m, m2)fmt.Println(b) // true
}

maps.EqualFunc

定义如下:

func EqualFunc[M1 ~map[K]V1, M2 ~map[K]V2, K comparable, V1, V2 any](m1 M1, m2 M2, eq func(V1, V2) bool) bool

类似于Equal函数,但使用自定义的 eq 函数进行比较。

package mainimport ("fmt""maps""strings"
)func main() {m1 := map[int]string{1:    "one",10:   "Ten",1000: "THOUSAND",}m2 := map[int][]byte{1:    []byte("One"),10:   []byte("Ten"),1000: []byte("Thousand"),}eq := maps.EqualFunc(m1, m2, func(v1 string, v2 []byte) bool {return strings.ToLower(v1) == strings.ToLower(string(v2))})fmt.Println(eq) // true
}

将值转换为小写的字符串后进行比较。

http://www.lryc.cn/news/147593.html

相关文章:

  • 《向量数据库指南》——腾讯云向量数据库(Tencent Cloud VectorDB) SDK 正式开源
  • Tutorial: Mathmatical Derivation of Backpropagation
  • 如何在 Linux 中设置 SSH 无密码登录
  • 什么时候用增量式PID,什么时候用位置式PID
  • Go语言入门记录:从基础到变量、函数、控制语句、包引用、interface、panic、go协程、Channel、sync下的waitGroup和Once等
  • 位运算进阶操作
  • sql:SQL优化知识点记录(四)
  • Java----Sentinel持久化规则启动
  • Java版工程行业管理系统源码-专业的工程管理软件- 工程项目各模块及其功能点清单
  • 无涯教程-Android - Grid View函数
  • 【第四阶段】kotlin语言的解构语法过滤元素
  • 和24考研说拜拜,不考研读中外合作办学硕士——人大女王金融硕士
  • https比http安全在哪
  • 基于Java的代驾管理系统 springboot+vue,mysql数据库,前台用户、商户+后台管理员,有一万五千字报告,完美运行
  • 广播、组播
  • Spring MVC 三 :基于注解配置
  • 机器学习基础16-建立预测模型项目模板
  • ReID网络:MGN网络(4) - Loss计算
  • CountDownLatch、Semaphore详解——深入探究CountDownLatch、Semaphore源码
  • windows生成ios证书的方法
  • 【小沐学Unity3d】3ds Max 骨骼动画制作(Physique 修改器)
  • 生态项目|Typus如何用Sui特性制作动态NFT为DeFi赋能
  • IOS打包上架AppStore被驳回信息记录
  • 【Python自学笔记】Python好用的模块收集(持续更新...)
  • 在springboot中配置mybatis(mybatis-plus)mapper.xml扫描路径的问题
  • c++搜索剪枝常见方法与技巧
  • YOLO V5 和 YOLO V8 对比学习
  • 【Git】(六)子模块跟随主仓库切换分支
  • 开源的经济影响:商业与社区的平衡
  • 数据库复习整理