最佳答案
我正在写一些代码,我需要它来捕捉参数并通过 fmt.Println
传递它们
(我希望它的默认行为是用空格分隔参数,后面跟一个换行符)。然而,它需要 []interface {}
,但是 flag.Args()
返回一个 []string
。
下面是代码示例:
package main
import (
"fmt"
"flag"
)
func main() {
flag.Parse()
fmt.Println(flag.Args()...)
}
This returns the following error:
./example.go:10: cannot use args (type []string) as type []interface {} in function argument
这是一个错误吗? fmt.Println
不应该采用 any数组吗? 顺便说一下,我也尝试过这样做:
var args = []interface{}(flag.Args())
but I get the following error:
cannot convert flag.Args() (type []string) to type []interface {}
有办法解决这个问题吗?