You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

34 lines
687 B

package model
import (
"fmt"
"reflect"
"strings"
)
// Implode function like php
func Implode(list interface{}, seq string) string {
listValue := reflect.Indirect(reflect.ValueOf(list))
if listValue.Kind() != reflect.Slice {
return ""
}
count := listValue.Len()
listStr := make([]string, 0, count)
for i := 0; i < count; i++ {
v := listValue.Index(i)
if str, err := getValue(v); err == nil {
listStr = append(listStr, str)
}
}
return strings.Join(listStr, seq)
}
func getValue(value reflect.Value) (res string, err error) {
switch value.Kind() {
case reflect.Ptr:
res, err = getValue(value.Elem())
default:
res = fmt.Sprint(value.Interface())
}
return
}