2016-12-04 03:10:47 -05:00
|
|
|
package serial
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2017-02-13 16:39:55 -05:00
|
|
|
// ToString serialize an abitrary value into string.
|
2016-12-04 03:10:47 -05:00
|
|
|
func ToString(v interface{}) string {
|
|
|
|
if v == nil {
|
|
|
|
return " "
|
|
|
|
}
|
|
|
|
|
|
|
|
switch value := v.(type) {
|
|
|
|
case string:
|
|
|
|
return value
|
|
|
|
case *string:
|
|
|
|
return *value
|
|
|
|
case fmt.Stringer:
|
|
|
|
return value.String()
|
|
|
|
case error:
|
|
|
|
return value.Error()
|
|
|
|
case []byte:
|
|
|
|
return BytesToHexString(value)
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%+v", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Concat(v ...interface{}) string {
|
|
|
|
values := make([]string, len(v))
|
|
|
|
for i, value := range v {
|
|
|
|
values[i] = ToString(value)
|
|
|
|
}
|
|
|
|
return strings.Join(values, "")
|
|
|
|
}
|
2016-12-06 05:03:42 -05:00
|
|
|
|
2016-12-09 07:17:34 -05:00
|
|
|
func WriteString(s string) func([]byte) (int, error) {
|
2016-12-09 06:08:25 -05:00
|
|
|
return func(b []byte) (int, error) {
|
|
|
|
return copy(b, []byte(s)), nil
|
2016-12-06 05:03:42 -05:00
|
|
|
}
|
|
|
|
}
|