dpbz14739
2015-10-09 06:29
浏览 2.0k
已采纳

如何在golang中创建对象数组?

I have a requirement in which I need to store array of objects in a variable. The objects are of different types. Refer to following example:

 v := [ {"name":"ravi"},
        ["art","coding","music","travel"],
        {"language":"golang"},
        {"experience":"no"}
      ]

Notice the second element is array of string itself. After research, I thought of storing this as interface type like:

 var v interface{} = [ {"name":"ravi"},
                       ["art","coding","music","travel"],
                       {"language":"golang"},
                       {"experience":"no"}
                     ]

Still, I am getting few compilation errors which I am not able to find out.

图片转代码服务由CSDN问答提供 功能建议

我有一个要求,我需要在变量中存储对象数组。 对象是不同类型的。 请参考以下示例:

  v:= [{“ name”:“ ravi”},
 [“ art”,“ coding”,“ music”,“ travel  “],
 {” language“:” golang“},
 {” experience“:” no“} 
] 
   
 
 

注意第二个元素 是字符串本身的数组。 经过研究,我想到了将其存储为以下接口类型:

  var v interface {} = [{“ name”:“ ravi”},
 [“ art”  ,“编码”,“音乐”,“旅行”],
 {“ language”:“ golang”},
 {“ experience”:“ no”} 
] 
    
 
 

仍然,我遇到了一些无法找到的编译错误。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题