24

この構造体にスライス タイプを追加する必要があります。

 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

これを修正するにはどうすればよいですか? 配列 (スライス) 型を含む構造体を作成するにはどうすればよいですか?

4

1 に答える 1

50

Example構造体の適切なスライスは次のとおりです。

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

説明させてください。のスライスを作りたいExample。だからここにあります — []Example{}. Example次に、 —を入力する必要がありますExample{}Example次に、[]string—で構成され[]string{"a", "b", "c"}ます。それは適切な構文の問題です。

それが役立つことを願っています。

于 2013-10-20T20:44:01.503 に答える