-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjoin.go
36 lines (30 loc) · 793 Bytes
/
join.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package goulash
import (
"fmt"
"reflect"
"strings"
"github.com/farbodsalimi/goulash/utils"
)
// Join creates and returns a new string by concatenating all of the elements in a slice, separated by a specified delimiter
func Join[T any](slice []T, delimiter string) string {
var result string
generic := utils.ParseGeneric(slice)
switch generic.Type.Elem().Kind() {
case reflect.Int,
reflect.Int8,
reflect.Int16,
reflect.Int32,
reflect.Int64,
reflect.Uint,
reflect.Uint8,
reflect.Uint16,
reflect.Uint32,
reflect.Uint64,
reflect.Float32,
reflect.Float64:
result = strings.Trim(strings.Join(strings.Fields(fmt.Sprint(slice)), delimiter), "[]")
case reflect.String:
result = strings.Join(generic.Value.Interface().([]string), delimiter)
}
return result
}