如果仅更改一种参数类型,则避免代码重复

Avoid code duplication if what changes is one argument type only

如您所见,这些函数仅在 keys []intkeys []*int(以及名称)方面有所不同。

func (db *myDB) QueryWithKeys(keys []int) ([]*models.Player) {
    var players = make([]*models.Player, len(keys))
  _ := db.Model(&players).Where("id in (?)", pg.In(keys)).Select()
  // several other equal code here
    return players
}

func (db *myDB) QueryWithPointerKeys(keys []*int) ([]*models.Player) {
    var players = make([]*models.Player, len(keys))
    _ := db.Model(&players).Where("id in (?)", pg.In(keys)).Select()
  // several other equal code here
    return players
}

有没有办法避免这种代码重复?

您可以做的是让 QueryWithKeys 方法调用 QueryWithPointerKeys 方法,然后放入一个指向键整数的指针。明显的缺点是处理时间会稍微长一些。

您可以重新定义该方法以接受 interface{} 类型的 keys 参数以及 int 类型的 length 参数,指示元素的数量keys 参数的底层切片,这个长度将由调用者提供。

func (db *myDB) QueryWithKeys(keys interface{}, length int) ([]*models.Player) {
    var players = make([]*models.Player, length)
    _ := db.Model(&players).Where("id in (?)", pg.In(keys)).Select()
    // several other equal code here
    return players
}

有了它,您就可以像这样使用它了:

var keys []int = // get list of integers from wherever
players := db.QueryWithKeys(keys, len(keys))

var keys2 []*int = // get list of integer pointers from wherever
players2 := db.QueryWithKeys(keys2, len(keys2))