表示功能,可任意分割 [英] Express function that takes any slice

查看:41
本文介绍了表示功能,可任意分割的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想表达一个可以取任何切片的函数.我以为我可以这样做:

I want to express a function that can take any slice. I thought that I could do this:

func myFunc(list []interface{}) {
  for _, i := range list {
    ...
    some_other_fun(i)
    ...
  }
}

其中 some_other_fun(..)本身采用 interface {} 类型.但是,这不起作用,因为您不能将 [] DEFINITE_TYPE 作为 [] interface {} 传递.请参阅: https://golang.org/doc/faq#convert_slice_of_interface ,其中指出了[]接口{}是不同的.该答案总结了为什么但关于接口指针而不是接口片,但原因是相同的:

where some_other_fun(..) itself takes an interface{} type. However, this doesn't work because you can't pass []DEFINITE_TYPE as []interface{}. See: https://golang.org/doc/faq#convert_slice_of_interface which notes that the representation of an []interface{} is different. This answer sums up why but with respect to pointers to interfaces instead of slices of interfaces, but the reason is the same: Why can't I assign a *Struct to an *Interface?.

上面的golang.org链接中提供的建议建议从 DEFINITE_TYPE 切片重建一个新的接口切片.但是,在我想调用该函数的代码中到处都不能做到这一点(此函数本身仅缩写9行代码,但是这9行在我们的代码中经常出现).

The suggestion provided at the golang.org link above suggests rebuilding a new interface slice from the DEFINITE_TYPE slice. However, this is not practical to do everywhere in the code that I want to call this function (This function is itself meant to abbreviate only 9 lines of code, but those 9 lines appear quite frequently in our code).

在每种情况下,我要调用该函数时,我都将传递一个 [] * DEFINITE_TYPE ,我最初认为它会更易于抽象,直到再次发现

In every case that I want to invoke the function I would be passing a []*DEFINITE_TYPE which I at first thought would be easier to abstract until, again, I discovered Why can't I assign a *Struct to an *Interface? (also linked above).

此外,每次我想使用不同的 DEFINITE_TYPE 调用该函数时,为n个类型实现n个示例将不会为我节省任何代码行,也不会使我的代码更清晰(相反!).

Further, everytime I want to invoke the function it is with a different DEFINITE_TYPE so implementing n examples for the n types would not save me any lines of code or make my code any clearer (quite the contrary!).

令人沮丧的是我无法执行此操作,因为9行代码在我们的代码中是惯用的,而且错误键入很容易引入错误.我真的很缺少泛型.真的没有办法吗?!

It is frustrating that I can't do this since the 9 lines are idiomatic in our code and a mistype could easily introduce a bug. I'm really missing generics. Is there really no way to do this?!!

推荐答案

可能最好的方法是定义一个接口,该接口封装 myFunc 需要对切片执行的操作(即,在您的例如,获取第n个元素).然后,该函数的参数就是该接口类型,然后为要传递给该函数的每种类型定义接口方法.

Probably the best thing to do is to define an interface that encapsulates what myFunc needs to do with the slice (i.e., in your example, get the nth element). Then the argument to the function is that interface type and you define the interface method(s) for each type you want to pass to the function.

您也可以使用 reflect 包来执行此操作,但这可能不是一个好主意,因为如果传递除slice(或数组或字符串)以外的其他内容,它将引起恐慌.

You can also do it with the reflect package, but that's probably not a great idea since it will panic if you pass something other than a slice (or array or string).

func myFunc(list interface{}) {
    listVal := reflect.ValueOf(list)
    for i := 0; i < listVal.Len(); i++ {
        //...
        some_other_fun(listVal.Index(i).Interface())
        //...
    }
}

请参见 https://play.golang.org/p/TyzT3lBEjB .

这篇关于表示功能,可任意分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆