【发布时间】:2019-01-06 21:02:12
【问题描述】:
我正在尝试编写一个可以采用任何类型的函数(更具体地说,试图让它采用任何类型的 protobuffer,但如果必须,我会满足于更广泛的通用性)。根据我的阅读,似乎可以这样做:
func processSlice(mySlice []interface{}) void{
// Do stuff in here
}
但是,当我尝试将它与一片 protos 一起使用时,我收到以下错误:
cannot use myProtoSlice (type []*MyProto) as type []interface{} in argument to processSlice
【问题讨论】:
-
[]interface{}不是interface{},它是一个特定类型(interface{}类型的一个片段)。您必须将[]interface{}传递给该函数,但该切片可以包含任何类型(或任何类型的混合)的值。 -
尝试使用类似
func processSlice(mySlice ...interface{}) void{ // Do stuff in here }
标签: generics go protocol-buffers