dpcj40970 2015-08-17 20:42
浏览 50
已采纳

Go-合并2个函数,唯一的区别是参数类型

I have those 2 almost exact functions :

Number 1:

func mergeSlicesOfRequestObjects(aSliceDestination *[]RequestObject, aSliceSource []RequestObject) {
for _, oSliceSourceItem := range aSliceSource {
    // Get current key
    iIndexSourceItemFound := -1
    for iIndexAttribute, oSliceDestinationItem := range *aSliceDestination {
        if oSliceSourceItem.Key == oSliceDestinationItem.Key {
            iIndexSourceItemFound = iIndexAttribute
            break
        }
    }

    // Update attribute
    if iIndexSourceItemFound == -1 {
        *aSliceDestination = append(*aSliceDestination, oSliceSourceItem)
    }
}
}

Number 2:

func mergeSlicesOfResponseObjects(aSliceDestination *[]ResponseObject, aSliceSource []ResponseObject) {
for _, oSliceSourceItem := range aSliceSource {
    // Get current key
    iIndexSourceItemFound := -1
    for iIndexAttribute, oSliceDestinationItem := range *aSliceDestination {
        if oSliceSourceItem.Key == oSliceDestinationItem.Key {
            iIndexSourceItemFound = iIndexAttribute
            break
        }
    }

    // Update attribute
    if iIndexSourceItemFound == -1 {
        *aSliceDestination = append(*aSliceDestination, oSliceSourceItem)
    }
}
}

As you can see the only difference is the struct type of the arguments of the functions.

So here is my question : is there any way to merge those 2 functions into one ?

I've tried using interfaces but I can't figure it out...

Thanks for all the answer in advance :)

Cheers

EDIT

I've implemented thwd answer but I'm getting errors. Here's what I've done:

type Request struct {
    Headers         []RequestObject
}

type RequestObject {
    Key string
}

type Keyer interface {
    GetKey() string
}

func (oRequestObject RequestObject) GetKey() string {
    return oRequestObject.Key
}

func mergeKeyers(aDst *[]Keyer, aSrc []Keyer) {
    // Logic
}

func test() {
    // rDst and rSrc are Request struct
    mergeKeyers(&rDst.Headers, rSrc.Headers)
}

And I'm getting the following errors when executing test():

cannot use &rDst.Headers (type *[]RequestObject) as type *[]Keyer in argument to mergeKeyers
cannot use rSrc.Headers (type []RequestObject) as type []Keyer in argument to mergeKeyers

Any idea why ?

  • 写回答

1条回答 默认 最新

  • dongtuanzi1080 2015-08-17 22:24
    关注

    Define an interface:

    type Keyer interface {
        Key() int // or whatever type the Key field has
    }
    

    Then implement the interface on both types:

    func (r RequestObject) Key() int {
        return r.Key
    }
    
    func (r ResponseObject) Key() int {
        return r.Key
    }
    

    And rewrite your function to take this interface (and not use hungarian notation and endlessly long variable names):

    func mergeKeyers(dst *[]Keyer, src []Keyer) {
        for _, s := range src {
            f := -1
            for i, d := range *dst {
                if s.Key() == d.Key() {
                    f = i
                    break
                }
            }
            if f == -1 {
                *dst = append(*dst, s)
            }
        }
    }
    

    Also, take into account Dave C's comment:

    you have an O(n×m) algorithm where you could use a O(n+m) one.

    I'll leave it up to you to optimize your code in that way.


    Edit to address your second question:

    The types *[]RequestObject and *[]Keyer are distinct and not interchangeable. What you need to do is convert your slice of RequestObjects to a slice of Keyers.

    This is as easy as iterating over the []RequestObject and assigning each entry to a new entry in a value of type []Keyer.

    See also these answers:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟