Convert int array to string separated by ','

Make IDs a []string and convert the integers when you append them

var IDs []string
for _, i := range []int{1, 2, 3, 4} {
    IDs = append(IDs, strconv.Itoa(i))
}

fmt.Println(strings.Join(IDs, ", "))

https://play.golang.org/p/xrfuMRjgiI


I would prefer to use json.Marshal. It is much simple and easy to use.

data := []int{100, 200, 300}
s, _ := json.Marshal(data)

fmt.Println(strings.Trim(string(s), "[]"))

GoPlaygroundLink

I hope this helps you. Please feel free to ask in case of doubts. WebsiteLink

Tags:

Go