2020-11-25 14:01:53 +03:00
|
|
|
package serial
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2022-02-03 03:57:32 +03:00
|
|
|
// ToString serializes an arbitrary value into string.
|
2020-11-25 14:01:53 +03:00
|
|
|
func ToString(v interface{}) string {
|
|
|
|
if v == nil {
|
2020-12-24 22:45:35 +03:00
|
|
|
return ""
|
2020-11-25 14:01:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch value := v.(type) {
|
|
|
|
case string:
|
|
|
|
return value
|
|
|
|
case *string:
|
|
|
|
return *value
|
|
|
|
case fmt.Stringer:
|
|
|
|
return value.String()
|
|
|
|
case error:
|
|
|
|
return value.Error()
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%+v", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Concat concatenates all input into a single string.
|
|
|
|
func Concat(v ...interface{}) string {
|
|
|
|
builder := strings.Builder{}
|
|
|
|
for _, value := range v {
|
|
|
|
builder.WriteString(ToString(value))
|
|
|
|
}
|
|
|
|
return builder.String()
|
|
|
|
}
|