我怎样才能在Go表达一个类型,是“(字符串或其他类似的列表)名单”“(字符串或‘其他如切片’)的片”的类型?基本上,好ol'“树表示为无限的列表和作为值的东西(在这个例子中的字符串)的东西”如何表达
我正在寻找一个S表达式的最简单的可能表示(它本身就是最简单的AST),在Python中看起来像这样:
sexp1 = ["+", "x", "y", ["*", "10", "myVal"]]
sexp2 = ["foo" "bar" "baz"]
sexp3 = [ [ [["gooo"], "moo"] ], "too", ["yoo", "2"] ]
这些表达式在Go中有什么类型?显然[][]string
不起作用,因为这不工作:
func makeSexp(parserName string, values ...[][]string) [][]string {
return append([]string{parserName}, values...)
}
(编译错误:1。cannot use values (type [][][]string) as type []string in append
,2 cannot use append([]string literal, values...) (type []string) as type [][]string in return argument
)
...而完全无类型版本的Works(但我不想完全放弃类型安全):
func makeSexp(parserName string, values ...interface{}) interface{} {
return append([]interface{}{parserName}, values...)
}
创建'ListHolder'和'StringHolder'类型。用两种方法定义接口:'What_type_are_you'和'Give_me_your_value'。 –