Skip to content

Instantly share code, notes, and snippets.

@vikashvikram
Created May 14, 2015 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vikashvikram/eef0edea6acdcf92f254 to your computer and use it in GitHub Desktop.
Save vikashvikram/eef0edea6acdcf92f254 to your computer and use it in GitHub Desktop.
Join integer array elements with a string e.g. [1,2,3,4] => "1, 2, 3, 4"
package main
import (
"fmt"
"strconv"
)
func Join(arr []int64, str string) string {
result := ""
last_index := len(arr) - 1
for i, v := range arr {
result = result + strconv.FormatInt(v, 10)
if i != last_index {
result = result + str
}
}
return result
}
func main() {
array := []int64{1,2,3,4}
fmt.Println(Join(array, ","))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment