GO函数参数中数组的通用类型
问题描述:
I have this function:
func functionX(collection []*interface{}) {
...
response, err := json.MarshalIndent(collection, "", " ")
...
}
I want the collection parameter to allow arrays of any kind, that's why I tried with *interface{} but I'm receiving errors like this:
cannot use MyDataType (type []*model.MyDataType) as type []*interface {} in argument to middleware.functionX
我具有以下功能: p>
func functionX(collection [ ] * interface {}){
...
响应,错误:= json.MarshalIndent(collection,“”,“”)
...
}
code> pre> \ n
我希望 collection strong>参数允许任何类型的数组,这就是为什么我尝试使用 * interface {} strong>的原因,但是我收到如下错误: p>
不能在中间件.functionX的参数中使用MyDataType(类型[] * model.MyDataType)作为类型[] * interface {}
code> pre>
div>
答
You can't do it that way, however you can easily do this:
func functionX(collection interface{}) error {
...
response, err := json.MarshalIndent(collection, "", " ")
...
}