2016-04-30 69 views
1

在Go中强制映射地图是否可行?我想要做的是一样的东西:在Go中强制映射类型

type IDMap map[string]bool 
type ObjectMap map[string]Object 

,然后写一个采取map[string]interface{}类型,这样的论点,我可以处理这两种基本类型作为参数,这样的功能:

func (im IDMap) Intersect(other map[string]interface{}) { 
    result = make(IDMap, 0) 
    for k := range im { 
     if _, ok := other[k]; ok { 
      result[k] = true 
     } 
    } 
    return result 
} 

func (om ObjectMap) Intersect(other map[string]interface{}) { 
    result = make(ObjectMap, 0) 
    for k := range om { 
     if _, ok := other[k]; ok { 
      result[k] = om[k] 
     } 
    } 
    return result 
} 

允许我使用任一类型的对象调用任一类型的相交方法。当我尝试时,我收到一个类型错误消息。是这样的可能吗?

+1

在Go号类型是不变的。可能的重复:http://stackoverflow.com/questions/12753805/type-converting-slices-of-interfaces-in-go – JimB

回答

0

尝试使用interface{}switch类型检查:

package main 

import (
    "fmt" 
) 

type IDMap map[string]bool 
type ObjectMap map[string]interface{} 
type Map interface { 
    Intersect(interface{}) Map 
} 

func main() { 
    im1 := IDMap{"a": true, "b": false, "c": true, "e": false} 
    other1 := IDMap{"b": true, "d": false, "e": true} 
    other2 := ObjectMap{"b": false, "x": 1, "y": 3.4, "z": "hello"} 
    other3 := map[string]interface{}{"m": 5.4, "n": false, "o": "world", "b": "foo", "a": "bar"} 

    overlap1 := im1.Intersect(other1) 
    check(overlap1) 

    overlap2 := im1.Intersect(other2) 
    check(overlap2) 

    overlap3 := im1.Intersect(other3) 
    check(overlap3) 


    im2 := ObjectMap{"a": "hello", "b": 4.5, "c": 10, "e": []string{"what?"}} 
    other4 := IDMap{"b": true, "d": false, "e": true} 
    other5 := ObjectMap{"b": false, "x": 1, "y": 3.4, "z": "hello"} 
    other6 := map[string]interface{}{"m": 5.4, "n": false, "o": "world", "b": "foo", "a": "bar"} 

    overlap4 := im2.Intersect(other4) 
    check(overlap4) 

    overlap5 := im2.Intersect(other5) 
    check(overlap5) 

    overlap6 := im2.Intersect(other6) 
    check(overlap6) 

} 

func check(m Map) { 
    fmt.Println(m) 
} 

func (im IDMap) Intersect(other interface{}) Map { 
    result := IDMap{} 

    switch o := other.(type) { 
    case IDMap: 
     for k := range im { 
      if _, ok := o[k]; ok { 
       result[k] = true 
      } 
     } 
    case ObjectMap: 
     for k := range im { 
      if _, ok := o[k]; ok { 
       result[k] = true 
      } 
     } 
    case map[string]interface{}: 
     for k := range im { 
      if _, ok := o[k]; ok { 
       result[k] = true 
      } 
     } 
    } 

    return result 
} 

func (om ObjectMap) Intersect(other interface{}) Map { 
    result := ObjectMap{} 

    switch o := other.(type) { 
    case IDMap: 
     for k := range om { 
      if _, ok := o[k]; ok { 
       result[k] = om[k] 
      } 
     } 
    case ObjectMap: 
     for k := range om { 
      if _, ok := o[k]; ok { 
       result[k] = om[k] 
      } 
     } 
    case map[string]interface{}: 
     for k := range om { 
      if _, ok := o[k]; ok { 
       result[k] = om[k] 
      } 
     } 
    } 

    return result 
}