douhezi2285 2016-04-30 00:49
浏览 6

在Go中强制映射类型

Is type coercion of maps possible in Go? What I'd like to do is something like:

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

and then write functions which take arguments of type map[string]interface{} so that I can handle either of these base types as an argument, like this:

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
}

allowing me to call the Intersect method of either type with an object of either type. When I try, I get a type error message. Is something like this possible?

  • 写回答

1条回答 默认 最新

  • dongshuxi3105 2016-04-30 02:19
    关注

    Try using interface{} and switch type checking:

    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
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错