donglisi8644 2016-04-06 17:49
浏览 82
已采纳

在Go中为多类型数组创建通用函数

I am trying to create a generic function that can handle actions on slices in Go... for instance, append an item of any type to a slice of that same type. This is simply a generic purpose for a more complex solution, but overall the issue boils down to this example:

package main

type car struct {
    make  string
    color string
}

type submarine struct {
    name   string
    length int
}

func genericAppender(thingList interface{}, thing interface{}) []interface{} {
    return append(thingList, thing)
}

func main() {

    cars := make([]car, 0, 10)
    cars[0] = car{make: "ford", color: "red"}
    cars[1] = car{make: "chevy", color: "blue"}

    subs := make([]submarine, 0, 10)
    subs[0] = submarine{name: "sally", length: 100}
    subs[1] = submarine{name: "matilda", length: 200}

    newCar := car{make: "bmw", color: "white"}
    genericAppender(&cars, newCar)

}

The code playground is at this location

The above errors as follows:

prog.go:14: first argument to append must be slice; have interface {}
  • 写回答

2条回答 默认 最新

  • douzai3399 2016-04-06 17:52
    关注

    After this change you're still getting a runtime error (index out of range) however the problem is that thingList is not of type []interface{} but rather interface{} so you can't append to it. Here's an updated version of your code on playground that does a type assertion to convert it to an []interface{} in line with the append. In reality you need to do that on a separate line and check for errors.

    https://play.golang.org/p/YMed0VDZrv

    So to put some code here;

    func genericAppender(thingList interface{}, thing interface{}) []interface{}  {
        return append(thingList.([]interface{}), thing)
    }
    

    will solve the basic problem you're facing. As noted, you still get runtime errors when indexing into the slice. Also, you could change the argument to avoid this by making it;

    func genericAppender(thingList []interface{}, thing interface{}) []interface{} {
        return append(thingList, thing)
    }
    

    Here's a complete example of the second type; https://play.golang.org/p/dIuW_UG7XY

    Note I also corrected the runtime error. When you use make with 3 args they are, in this order, type, length, capacity. This means the length of the array is 0 so when you try to assign to indexes 0 and 1 it was causing a panic for IndexOutoFRange. Instead I removed the middle argument so it's make([]interface{}, 10) meaning the length is initially set to 10 so you can assign to those indexes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址