diff --git a/app/utils/convert.go b/app/utils/convert.go index 6a39371..28f97a7 100644 --- a/app/utils/convert.go +++ b/app/utils/convert.go @@ -137,10 +137,39 @@ func AnyToString(raw interface{}) string { return i.Error() case bool: return strconv.FormatBool(i) + case []interface{}: + return interfaceSliceToJSONStyleString(i) } return fmt.Sprintf("%#v", raw) } +func interfaceSliceToJSONStyleString(slice []interface{}) string { + var elements []string + + for _, v := range slice { + switch value := v.(type) { + case string: + // 对字符串进行转义并加上双引号 + elements = append(elements, fmt.Sprintf("%q", value)) + case bool: + // 直接转换为字符串 + elements = append(elements, fmt.Sprintf("%v", value)) + case float64, float32: + // 格式化浮点数 + elements = append(elements, fmt.Sprintf("%v", value)) + case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: + // 格式化整数 + elements = append(elements, fmt.Sprintf("%v", value)) + default: + // 其他类型也尽量尝试转换为字符串 + elements = append(elements, fmt.Sprintf("%v", value)) + } + } + + // 使用方括号包裹,并用逗号分隔各个元素 + return "[" + strings.Join(elements, ", ") + "]" +} + func AnyToFloat64(raw interface{}) float64 { switch i := raw.(type) { case []byte: