-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)。 –