Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Go,Golang:结构内部的数组类型,缺少类型复合文字_Go - Fatal编程技术网

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足够聪明,可以发现
[]示例
中充满了
示例
项,而无需明确命名。