我正在寻找一种优雅的方式来解压缩 Go 中的参数列表。我不想为此目的使用可变参数函数,因为在我的用例中,在编写函数时我已经知道参数的数量,我想保持这部分简单。但是在我的用例中,参数以[]interface{}. 我找不到解决方案,但嘿,也许有人已经知道如何做到这一点?
package main
import (
"fmt"
)
// NON-VARIADIC greater
func greet(n1, n2 string) {
fmt.Printf("%s %s\n", n1, n2)
}
func main() {
l := []interface{}{"hello", "world"}
// works
greet(l[0].(string), l[1].(string))
// does not work: "./args.go:20: not enough arguments in call to greet"
//greet(l...)
// is there something more elegant to unzip the list?
}
幕布斯7119047
MM们
相关分类