08-Map拓展

87 阅读1分钟

Map与工厂模式

  • Map的value可以是一个方法
  • 与Go的Dock type接口方式一起,可以方便地实现单一方法对象的工厂模式
func TestMapWithFunValue(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))
}

实现Set

Go的内置集合中没有Set的实现,可以map[type]bool实现

  1. 元素唯一性

  2. 基本操作

    • 添加元素
    • 判断元素是否存在
    • 删除元素
    • 元素个数
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)
    }
}