I want to write a function that can convert slice([]int, []string, []bool, []int64, []float64) to string.
[]string{a,b,c} -> a,b,c []int{1,2,3} -> 1,2,3 There is my code:
func sliceToString(itr interface{}) string { switch itr.(type) { case []string: return strings.Join(itr.([]string), ",") case []int: s := []string{} for _, v := range itr.([]int) { s = append(s, fmt.Sprintf("%v", v)) } return strings.Join(s, ",") case []int64: s := []string{} for _, v := range itr.([]int64) { s = append(s, fmt.Sprintf("%v", v)) } return strings.Join(s, ",") case []float64: s := []string{} for _, v := range itr.([]float64) { s = append(s, fmt.Sprintf("%v", v)) } return strings.Join(s, ",") case []bool: s := []string{} for _, v := range itr.([]bool) { s = append(s, fmt.Sprintf("%v", v)) } return strings.Join(s, ",") } return "" } But it's a little complicated, if i can convert interface{}(type is slice) to []interface{} or get element , it's getting more simple.
func sliceToString(itr interface{}) string { s := []string{} // convert interface{} to []interface{} or get elements // els := ... for _,v:= range els{ s = append(s, fmt.Sprintf("%v", v)) } return s }