Created
May 14, 2015 16:43
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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