1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-27 12:36:34 -05:00
v2fly/common/serial/string.go

36 lines
608 B
Go
Raw Normal View History

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 {
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
}