func testmap(t *testing.t) { m := map[int]func(op int) int{} m[1] = func(op int) int { return op } m[2] = func(op int) int { return op * op } m[3] = func(op int) int { return op * op * op } t.log(m[1](2), m[2](2), m[3](2)) }
输出
=== run testmap --- pass: testmap (0.00s) map_test.go:10: 2 4 8 pass process finished with exit code 0
go的内置集合中没有set实现,可以map[type]bool
func testmapforset(t *testing.t) { myset := map[int]bool{} myset[1] = true n := 3 if myset[n] { t.logf("%d is existing", n) } else { t.logf("%d is not existing", n) } myset[3] = true t.log(len(myset)) delete(myset,1) n = 1 if myset[n] { t.logf("%d is existing", n) } else { t.logf("%d is not existing", n) } }
输出
=== run testmapforset --- pass: testmapforset (0.00s) map_test.go:20: 3 is not existing map_test.go:23: 2 map_test.go:29: 1 is not existing pass process finished with exit code 0
示例代码请访问:
如对本文有疑问, 点击进行留言回复!!
VSCode1.4 搭建Golang的开发调试环境(遇到很多问题)
网友评论