2016-04-30 10 views
1

Goでのマップの型強制は可能ですか?Goでの強制的なマップタイプ

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 
} 
:その後、

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

と、このように、私は、引数としてこれらの基本型のいずれかを扱うことができるようにタイプmap[string]interface{}の引数を取る関数を書く:私は何をしたいことのようなものがあります

どちらのタイプのオブジェクトでもIntersectメソッドを呼び出すことができます。試してみると、タイプエラーメッセージが表示されます。このようなことは可能でしょうか?

+1

番タイプは不変です。可能な重複: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 
} 
関連する問題