如何在golang中声明一个函数接受一个接口数组?

我想声明一个函数接受接口数组,例如:

func (this *CvStoreServiceImpl) setItemList(coll *mgo.Collection, itemList ...interface{}) (err error)

Howerver,当我将此函数称为跟随失败时:

jobList := cvRaw.GetJobList()
this.setItemList(jobColl, jobList...)

这里有错误:

cannot use cvRaw.GetJobList() (type []*cv_type.CvJobItemRaw) as type []interface {} in argument to this.setItemList

最佳答案 我想你要找的是 is this

package main

import "fmt"

func main() {
    interfacetious := []interface{}{"s", 123, float64(999)}
    stuff(interfacetious)
    stuff2(interfacetious...)

    stuff2("or", 123, "separate", float64(99), "values")
}

// Stuff can only work with slice of things
func stuff(s []interface{}) {
    fmt.Println(s)
}

// Stuff2 is polyvaridc and can handle individual vars, or a slice with ...
func stuff2(s ...interface{}) {
    fmt.Println(s)
}
点赞