Go,Golang:结构内部的数组类型,缺少类型复合文字
我需要将切片类型添加到此结构中Go,Golang:结构内部的数组类型,缺少类型复合文字,go,Go,我需要将切片类型添加到此结构中 type Example struct { text []string } func main() { var arr = []Example { {{"a", "b", "c"}}, } fmt.Println(arr) } 那我就要 prog.go:11: missing type in composite literal [process exited with non-zero
type Example struct {
text []string
}
func main() {
var arr = []Example {
{{"a", "b", "c"}},
}
fmt.Println(arr)
}
那我就要
prog.go:11: missing type in composite literal
[process exited with non-zero status]
因此,指定复合文字
var arr = []Example {
{Example{"a", "b", "c"}},
但仍然会出现以下错误:
cannot use "a" (type string) as type []string in field value
我该如何解决这个问题?如何构造包含数组(切片)类型的结构?以下是您的
示例结构的正确切片:
[]Example{
Example{
[]string{"a", "b", "c"},
},
}
让我解释一下。您想对示例
进行切片。这里是-[]示例{}
。然后必须用示例
-示例{}
填充它<代码>示例
依次由[]字符串
-[]字符串{“a”、“b”、“c”}
组成。这只是正确语法的问题
希望有帮助。这个怎么样?它有更多的价值,并且因为另一个原因而不起作用。无法理解如果不使用文字符号命名字段,则必须为所有字段提供一个值,否则命名字段。基于您的工作示例:请注意,您不需要第二个示例
-go足够聪明,可以发现[]示例
中充满了示例
项,而无需明确命名。