package main import ( "encoding/json" "fmt" "reflect" "strconv" "time" ) // package main // import ( // "testing" // "time" // ) // func TestInterfaceToString(t *testing.T) { // tests := []struct { // name string // input interface{} // want string // }{ // {"nil", nil, ""}, // {"string", "hello", "hello"}, // {"bytes", []byte("world"), "world"}, // {"custom stringer", customStringer{}, "custom stringer implementation"}, // {"error", fmt.Errorf("test error"), "test error"}, // {"bool true", true, "true"}, // {"bool false", false, "false"}, // {"int", 42, "42"}, // {"int8", int8(8), "8"}, // {"int16", int16(16), "16"}, // {"int32", int32(32), "32"}, // {"int64", int64(64), "64"}, // {"uint", uint(42), "42"}, // {"uint8", uint8(8), "8"}, // {"uint16", uint16(16), "16"}, // {"uint32", uint32(32), "32"}, // {"uint64", uint64(64), "64"}, // {"float32", float32(3.14), "3.14"}, // {"float64", 3.1415, "3.1415"}, // {"complex64", complex64(1 + 2i), "(1+2i)"}, // {"complex128", 1 + 3i, "(1+3i)"}, // {"time", time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC), "2020-01-01T00:00:00Z"}, // {"duration", time.Hour, "1h0m0s"}, // {"slice", []int{1, 2, 3}, "[1,2,3]"}, // {"array", [2]string{"a", "b"}, `["a","b"]`}, // {"map", map[string]int{"a": 1}, `{"a":1}`}, // {"struct", struct{ A int }{42}, `{"A":42}`}, // {"pointer to int", intPtr(42), "42"}, // {"pointer to nil", (*int)(nil), ""}, // {"custom type", TestStr("custom"), "custom"}, // } // for _, tt := range tests { // t.Run(tt.name, func(t *testing.T) { // if got := InterfaceToString(tt.input); got != tt.want { // t.Errorf("InterfaceToString() = %v, want %v", got, tt.want) // } // }) // } // } // func intPtr(i int) *int { // return &i // } // InterfaceToString 将任意interface转换为string func InterfaceToString(v interface{}) string { if v == nil { return "" } // 获取值的反射对象 val := reflect.ValueOf(v) // 处理指针类型:解引用直到获取到非指针值 for val.Kind() == reflect.Ptr { if val.IsNil() { return "" } val = val.Elem() } // 获取解引用后的实际值 actualValue := val.Interface() // 根据具体类型进行处理 switch actual := actualValue.(type) { case string: return actual case []byte: return string(actual) case fmt.Stringer: return actual.String() case error: return actual.Error() case bool: return strconv.FormatBool(actual) case int: return strconv.Itoa(actual) case int8: return strconv.FormatInt(int64(actual), 10) case int16: return strconv.FormatInt(int64(actual), 10) case int32: return strconv.FormatInt(int64(actual), 10) case int64: return strconv.FormatInt(actual, 10) case uint: return strconv.FormatUint(uint64(actual), 10) case uint8: return strconv.FormatUint(uint64(actual), 10) case uint16: return strconv.FormatUint(uint64(actual), 10) case uint32: return strconv.FormatUint(uint64(actual), 10) case uint64: return strconv.FormatUint(actual, 10) case float32: return strconv.FormatFloat(float64(actual), 'f', -1, 32) case float64: return strconv.FormatFloat(actual, 'f', -1, 64) case complex64: return fmt.Sprint(actual) case complex128: return fmt.Sprint(actual) case time.Time: return actual.Format(time.RFC3339) case time.Duration: return actual.String() } // 处理切片、数组、map、结构体等复杂类型 - 使用JSON序列化 if val.IsValid() { switch val.Kind() { case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct: jsonBytes, err := json.Marshal(actualValue) if err == nil { return string(jsonBytes) } } } // 默认处理:使用fmt.Sprint return fmt.Sprint(actualValue) } // 示例用法 func main() { // 测试各种类型 testCases := []interface{}{ nil, "hello", 42, 3.14, true, []byte("world"), []int{1, 2, 3}, map[string]interface{}{"name": "John", "age": 30}, struct { Name string `json:"name"` Age int `json:"age"` }{"Alice", 25}, time.Now(), time.Hour * 2, // 指针类型 &[]string{"a", "b", "c"}, // 自定义Stringer customStringer{}, // 错误类型 fmt.Errorf("test error"), TestStrOne, 2454528425152485425, } for i, tc := range testCases { result := InterfaceToString(tc) fmt.Printf("Case %d: %T -> %s\n", i+1, tc, result) } } // 自定义Stringer实现 type customStringer struct{} func (c customStringer) String() string { return "custom stringer implementation" } type TestStr string const ( TestStrOne TestStr = "1" )