-1
我想在字符串列表中传递一个通用参数,但不确定它是否可能。我有解决方法,但觉得我只是无法得到正确的语法。从字符串列表到字符串中的接口列表
package main
import "fmt"
func Set(otherFields ...interface{}) {
fmt.Printf("%v", otherFields)
}
func main() {
a := []string {"Abc", "def", "ghi"}
Set(a) // incorrect behavior because a passed through as a list, rather than a bunch of parameters
// Set(a...) // compiler error: cannot use a (type []string) as type []interface {} in argument to Set
// Set([]interface{}(a)) // compiler error: cannot convert a (type []string) to type []interface {}
// This works but I want to do what was above.
b := []interface{} {"Abc", "def", "ghi"}
Set(b...)
}
请参阅[常见问题](https://golang.org/doc/faq#convert_slice_of_interface)。 –