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

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


// 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 "?"

}

这完美地完成了它的工作,但是看看实际的算法,我对代码重复的数量感到恼火,不幸的是 afallthrough在类型切换语句中不起作用(请参阅为什么在类型切换中不允许 fallthrough?)。


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


牛魔王的故事
浏览 78回答 1
1回答

狐的传说

更短的代码反映: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 "?"    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go