dsomm80482
2016-06-09 13:50
浏览 789
已采纳

在go中用逗号分割字符串后,获得[]接口{}

I have two ways to get a list.

  1. via api - which returns me a map[string]interface{}. The value I am interested in is stored in interface as []interface{} (array of strings which are by default set to array of interface)
  2. via static string - if api fails. This is a comma separated string. I use the strings.Split function which results in an []string

The above results in data of two types []interface{} and []string which is not compatible for the operations an I will have to perform same operation separately based on the type.

Further I am converting the array to a map[string]int with key as string and value as 1. I am converting it to a map for quick checking of some attributes in the original array

Is there any solution to the problem and what is the best way to do it..

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

我有两种获取列表的方法。

  1. 通过api-向我返回一个map [string] interface {}。 我感兴趣的值以[] interface {}(字符串数组,默认情况下设置为接口数组)的形式存储在接口中
  2. 通过静态字符串-如果api失败。 这是一个逗号分隔的字符串。 我使用strings.Split函数产生[] string

    上面的结果产生两种类型的数据:[] interface {}和[] string 与操作不兼容,我将不得不根据类型分别执行相同的操作。

    此外,我将数组转换为map [string] int,其键为字符串,值为1。我将其转换为地图,以便快速检查原始数组中的某些属性

    有没有解决问题的方法,什么是最好的解决方法。.

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

1条回答 默认 最新

相关推荐 更多相似问题