Created
October 11, 2016 23:32
-
-
Save ik5/9cf637393ccad659fdd2050f9725d9b9 to your computer and use it in GitHub Desktop.
Example on how to create join function for int64 - originally created it here: https://play.golang.org/p/KpdkVS1B4s
This file contains hidden or 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 UJoin(list []uint64, sep string) string { | |
length := len(list) | |
if length == 0 { | |
return "" | |
} | |
if length == 1 { | |
return strconv.FormatUint(list[0], 10) | |
} | |
s := "" | |
for i, item := range list { | |
s = s + strconv.FormatUint(item, 10) | |
if i < length-1 { | |
s = s + sep | |
} | |
} | |
return s | |
} | |
func Join(list []int64, sep string) string { | |
length := len(list) | |
if length == 0 { | |
return "" | |
} | |
if length == 1 { | |
return strconv.FormatInt(list[0], 10) | |
} | |
s := "" | |
for i, item := range list { | |
s = s + strconv.FormatInt(item, 10) | |
if i < length-1 { | |
s = s + sep | |
} | |
} | |
return s | |
} | |
func main() { | |
a := []int64{1, 5, 4, 12} | |
fmt.Println("Join: ", Join(a, ", ")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment