如何减少 Go 中将基本类型转换为字符串的函数中的代码重复

How to reduce code duplication in function which converts basic types to string in Go

我在 Go (1.18.1) 中编写了一个简单的函数,它将 any (a.k.interface{}) 类型作为输入,并将 returns 值作为字符串,如果是 nil 指针,函数 returns "nil";如果不支持该类型,则函数 returns "?"

// small helper funcion which is usefull when printing (pointer) values
func ToString(T any) string {
    switch v := T.(type) {
    case string:
        return v
    case *string:
        if v == nil {
            return "nil"
        } else {
            return *v
        }

    case int:
        return strconv.FormatInt(int64(v), 10)
    case int8:
        return strconv.FormatInt(int64(v), 10)
    case int16:
        return strconv.FormatInt(int64(v), 10)
    case int32:
        return strconv.FormatInt(int64(v), 10)
    case int64:
        return strconv.FormatInt(v, 10)
    case float32:
        return fmt.Sprintf("%f", v)
    case float64:
        return fmt.Sprintf("%f", v)

    case *int:
        if v == nil {
            return "nil"
        }
        return strconv.FormatInt(int64(*v), 10)
    case *int8:
        if v == nil {
            return "nil"
        }
        return strconv.FormatInt(int64(*v), 10)
    case *int16:
        if v == nil {
            return "nil"
        }
        return strconv.FormatInt(int64(*v), 10)
    case *int32:
        if v == nil {
            return "nil"
        }
        return strconv.FormatInt(int64(*v), 10)
    case *int64:
        if v == nil {
            return "nil"
        }
        return strconv.FormatInt(*v, 10)
    case *float32:
        if v == nil {
            return "nil"
        }
        return fmt.Sprintf("%f", *v)
    case *float64:
        if v == nil {
            return "nil"
        }
        return fmt.Sprintf("%f", *v)

    case *bool:
        if v == nil {
            return "nil"
        }
    case bool:
        if v == true {
            return "true"
        }
        return "false"
    }
    return "?"
}

这完美地完成了它的工作,但看看实际的算法,我对代码重复的数量感到恼火,不幸的是 fallthrough 在类型切换语句中不起作用(参见 Why isn't fallthrough allowed in a type switch?).

是否有更有效的方法(即代码重复更少)来完成与上述 ToString(T any) 函数相同的事情?

带有反射的更短代码:

func ToString(x any) string {
    v := reflect.ValueOf(x)
    if v.Kind() == reflect.Ptr {
        if v.IsZero() {
            return "nil"
        }
        v = v.Elem()
    }

    switch v.Kind() {
    case reflect.String:
        return v.String()
    case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
        return strconv.FormatInt(v.Int(), 10)
    case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
        return strconv.FormatUint(v.Uint(), 10)
    case reflect.Float32, reflect.Float64:
        return strconv.FormatFloat(v.Float(), 'f', -1, v.Type().Bits())
    case reflect.Bool:
        return strconv.FormatBool(v.Bool())
    default:
        return "?"
    }
}