github.com/eagleql/xray-core@v1.4.4/common/serial/string.go (about)

     1  package serial
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // ToString serialize an arbitrary value into string.
     9  func ToString(v interface{}) string {
    10  	if v == nil {
    11  		return ""
    12  	}
    13  
    14  	switch value := v.(type) {
    15  	case string:
    16  		return value
    17  	case *string:
    18  		return *value
    19  	case fmt.Stringer:
    20  		return value.String()
    21  	case error:
    22  		return value.Error()
    23  	default:
    24  		return fmt.Sprintf("%+v", value)
    25  	}
    26  }
    27  
    28  // Concat concatenates all input into a single string.
    29  func Concat(v ...interface{}) string {
    30  	builder := strings.Builder{}
    31  	for _, value := range v {
    32  		builder.WriteString(ToString(value))
    33  	}
    34  	return builder.String()
    35  }