Go,Golang:数组类型内部结构,缺少类型复合文字 [英] Go, Golang : array type inside struct, missing type composite literal
问题描述
type示例结构{
text [] string
func main(){
var arr = []示例{
{{a,b,c}},
fmt.Println(arr)
}
然后我获取
prog.go:11:复合字面值缺失类型
[退出时处于非零状态]
因此,指定复合文字
var arr = []示例{
{示例{a,b,c}},
但是仍然出现这个错误:
不能使用a(类型字符串)作为字段值中的类型[]字符串
http://play.golang.org/p/XKv1uhgUId
我如何解决这个问题?如何构造包含数组(slice)类型的结构?
这是您正确切分的示例
struct:
[]示例{
示例{
[ ] string {a,b,c},
},
}
让我解释一下。你想分割例子
。所以这里是 - []示例{}
。然后它必须填充示例
- 示例{}
。 示例
依次由 [] string -
[] string {a, b,c}
。这只是正确的语法问题。
希望有帮助。
I need to add slice type to this struct.
type Example struct {
text []string
}
func main() {
var arr = []Example {
{{"a", "b", "c"}},
}
fmt.Println(arr)
}
Then I am getting
prog.go:11: missing type in composite literal
[process exited with non-zero status]
So specify the composite literal
var arr = []Example {
{Example{"a", "b", "c"}},
But still getting this error:
cannot use "a" (type string) as type []string in field value
http://play.golang.org/p/XKv1uhgUId
How do I fix this? How do I construct the struct that contains array(slice) type?
Here is your proper slice of Example
struct:
[]Example{
Example{
[]string{"a", "b", "c"},
},
}
Let me explain it. You want to make a slice of Example
. So here it is — []Example{}
. Then it must be populated with an Example
— Example{}
. Example
in turn consists of []string
— []string{"a", "b", "c"}
. It just the matter of proper syntax.
Hope that helps.
这篇关于Go,Golang:数组类型内部结构,缺少类型复合文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!