dpcj40970 2015-08-17 12: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 14: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:

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

报告相同问题?

悬赏问题

  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
  • ¥15 我如果只想表示节点的结构信息,使用GCN方法不进行训练可以吗
  • ¥15 QT6将音频采样数据转PCM
  • ¥15 下面三个文件分别是OFDM波形的数据,我的思路公式和我写的成像算法代码,有没有人能帮我改一改,如何解决?
  • ¥15 Ubuntu打开gazebo模型调不出来,如何解决?
  • ¥100 有chang请一位会arm和dsp的朋友解读一个工程