在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..

Use the following code to create a map[string]int from comma separated values in a string s:

m := make(map[string]int)
for _, p := range strings.Split(s, ",") {
   m[p] = 1
}

Use the following code to create a map[string]int from values of type []interface{}:

m := make(map[string]int)
for _, v := range values {
   s, ok := v.(string)
   if !ok {
      // not a string, handle error
   }
   m[s] = 1
}