I have two ways to get a list.
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
}