2016-12-04 03:10:47 -05:00
|
|
|
package serial
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2018-11-06 22:15:47 -05:00
|
|
|
// ToString serialize an arbitrary value into string.
|
2016-12-04 03:10:47 -05:00
|
|
|
func ToString(v interface{}) string {
|
|
|
|
if v == nil {
|
2020-12-16 16:03:26 -05:00
|
|
|
return ""
|
2016-12-04 03:10:47 -05: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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 16:46:01 -05:00
|
|
|
// Concat concatenates all input into a single string.
|
2016-12-04 03:10:47 -05:00
|
|
|
func Concat(v ...interface{}) string {
|
2018-10-01 06:42:14 -04:00
|
|
|
builder := strings.Builder{}
|
|
|
|
for _, value := range v {
|
|
|
|
builder.WriteString(ToString(value))
|
2016-12-04 03:10:47 -05:00
|
|
|
}
|
2018-10-01 06:42:14 -04:00
|
|
|
return builder.String()
|
2016-12-04 03:10:47 -05:00
|
|
|
}
|