利用Go的反射,实现不同类型转字符串功能,从而实现泛型转字符串功能,并做了单元测试和性能测试。
package gotest
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"testing"
)
func
FormatString(iface
interface{})
string {
switch
val := iface.(type) {
case []
byte:
return
string(val)
}
v := reflect.
ValueOf(iface)
switch v.
Kind() {
case reflect.Invalid:
return
""