加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-16 09:23:16 所属栏目:大数据 来源:网络整理
导读:我想声明一个函数接受接口数组,例如: func (this *CvStoreServiceImpl) setItemList(coll *mgo.Collection,itemList ...interface{}) (err error) Howerver,当我将此函数称为跟随失败时: jobList := cvRaw.GetJobList()this.setItemList(jobColl,jobList..
我想声明一个函数接受接口数组,例如:

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","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)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读