Go, Golang : array type inside struct, missing type composite literal

前端 未结 1 1645
太阳男子
太阳男子 2021-02-12 15:14

I need to add slice type to this struct.

 type Example struct {
    text  []string
 }

 func main() {
    var arr = []Example {
        {{\"a\", \"b\", \"c\"}},
         


        
相关标签:
1条回答
  • 2021-02-12 15:27

    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 ExampleExample{}. Example in turn consists of []string[]string{"a", "b", "c"}. It just the matter of proper syntax.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题