15 go
我需要在此结构中添加切片类型.
type Example struct {
text []string
}
func main() {
var arr = []Example {
{{"a", "b", "c"}},
}
fmt.Println(arr)
}
Run Code Online (Sandbox Code Playgroud)
然后我就到了
prog.go:11: missing type in composite literal
[process exited with non-zero status]
Run Code Online (Sandbox Code Playgroud)
所以指定复合文字
var arr = []Example {
{Example{"a", "b", "c"}},
Run Code Online (Sandbox Code Playgroud)
但仍然收到此错误:
cannot use "a" (type string) as type []string in field value
Run Code Online (Sandbox Code Playgroud)
http://play.golang.org/p/XKv1uhgUId
我该如何解决?如何构造包含数组(切片)类型的结构?
Kav*_*avu 40
这是你正确的Example结构片段:
[]Example{
Example{
[]string{"a", "b", "c"},
},
}
Run Code Online (Sandbox Code Playgroud)
让我解释一下.你想做一个Example.所以这是 - []Example{}.然后它必须填充Example- Example{}.Example反过来由[]string- []string{"a", "b", "c"}.这只是正确语法的问题.
希望有所帮助.