2016-04-30 70 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 
}