Go,Golang:结构内的数组类型,缺少类型复合文字

2024-01-31

我需要向该结构添加切片类型。

 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 http://play.golang.org/p/XKv1uhgUId

我该如何解决?如何构造包含数组(切片)类型的结构?


这是你正确的部分Example struct:

[]Example{
  Example{
   []string{"a", "b", "c"},
  },
}

让我解释一下。你想做一片Example。所以这是——[]Example{}。然后它必须填充一个ExampleExample{}. Example依次包括[]string[]string{"a", "b", "c"}。这只是正确语法的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Go,Golang:结构内的数组类型,缺少类型复合文字 的相关文章

随机推荐