github.com/saucelabs/saucectl@v0.175.1/internal/slice/slice.go (about)

     1  package slice
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"strings"
     7  )
     8  
     9  // Join concatenates the elements of its first argument to create a single string. The separator string sep is placed
    10  // between elements in the resulting string.
    11  //
    12  // Disclaimer: Use this function judiciously.
    13  func Join(value any, sep string) string {
    14  	if reflect.TypeOf(value).Kind() != reflect.Slice {
    15  		return fmt.Sprintf("%v", value)
    16  	}
    17  
    18  	switch reflect.TypeOf(value).Elem().Kind() {
    19  	case reflect.String:
    20  		return strings.Join(value.([]string), sep)
    21  	case reflect.Interface:
    22  		elems := value.([]interface{})
    23  		var vv []string
    24  		for _, v := range elems {
    25  			vv = append(vv, fmt.Sprintf("%v", v))
    26  		}
    27  		return strings.Join(vv, sep)
    28  	case reflect.Int:
    29  		elems := value.([]int)
    30  		var vv []string
    31  		for _, v := range elems {
    32  			vv = append(vv, fmt.Sprintf("%d", v))
    33  		}
    34  		return strings.Join(vv, sep)
    35  	default:
    36  		return fmt.Sprintf("%v", value)
    37  	}
    38  }