我需要将切片类型添加到这个结构体中。
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
http://play.golang.org/p/XKv1uhgUId
我应该如何修复这个问题?如何构建包含数组(切片)类型的结构体?
[]Example
中包含了哪些Example
项,而无需显式命名它们。Playground - Nick Craig-Wood