如何在拥有接口数组时调用具有变体接口的方法?

huangapple go评论99阅读模式
英文:

How to invoke methods with variant interface when you have array of interface?

问题

如何调用以下签名的方法:

SomeFunc(args ...interface{})

使用类型为[]interface{}的变量,是否可以调用上述方法?如果可以,如何调用?

谢谢。

英文:

how to invoke a method with below signature

SomeFunc( args ...interface{})

with variable of type []interface{} Is it possible to invoke the above method? If yes how?

Thanks

答案1

得分: 2

func main() {
b := []interface{}{"你好", "嗨"}
SomeFunc(b...)
}

通过在b数组后面使用...解决了这个问题。

有关更多详细信息,请参见https://stackoverflow.com/questions/39914447/unpacking-slice-of-slices/39915311#39915311和https://stackoverflow.com/questions/36125024/golang-join-array-interface/36125119#36125119

英文:
func main() {
	b := []interface{}{"hello", "Hi"}
	SomeFunc(b...) 
}

Solved the issue by using ... after b array. <BR><BR>For more details plz see https://stackoverflow.com/questions/39914447/unpacking-slice-of-slices/39915311#39915311 and https://stackoverflow.com/questions/36125024/golang-join-array-interface/36125119#36125119

huangapple
  • 本文由 发表于 2016年12月29日 16:27:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/41375797.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定