Golang 标准库提供了很多类型转换的函数,如 strconv 包可完成 string 与基本数据类型之间的转换。
比如将 int 与 string 之间的互转。
// int to string
s := strconv.Itoa(i)// string to int
i, err := strconv.ParseInt(i, 0, 64)
如果我们想完成任意类型到某一具体类型的转换,该如何实现呢?
2.To String以 string 为,我们可以这样实现。
// ToStringE casts any type to a string type.
func ToStringE(i any) (string, error) {i = indirectToStringerOrError(i)switch s := i.(type) {case string:return s, nilcase bool:return strconv.FormatBool(s), nilcase float64:return strconv.FormatFloat(s, 'f', -1, 64), nilcase float32:return strconv.FormatFloat(float64(s), 'f', -1, 32), nilcase int:return strconv.Itoa(s), nilcase int64:return strconv.FormatInt(s, 10), nilcase int32:return strconv.Itoa(int(s)), nilcase int16:return strconv.FormatInt(int64(s), 10), nilcase int8:return strconv.FormatInt(int64(s), 10), nilcase uint:return strconv.FormatUint(uint64(s), 10), nilcase uint64:return strconv.FormatUint(uint64(s), 10), nilcase uint32:return strconv.FormatUint(uint64(s), 10), nilcase uint16:return strconv.FormatUint(uint64(s), 10), nilcase uint8:return strconv.FormatUint(uint64(s), 10), nilcase json.Number:return s.String(), nilcase []byte:return string(s), nilcase template.HTML:return string(s), nilcase template.URL:return string(s), nilcase template.JS:return string(s), nilcase template.CSS:return string(s), nilcase template.HTMLAttr:return string(s), nilcase nil:return "", nilcase fmt.Stringer:return s.String(), nilcase error:return s.Error(), nildefault:return "", fmt.Errorf("unable to cast %#v of type %T to string", i, i)}
}
其中 indirectToStringerOrError 是对指针类型的解引用,从标准库 html/template/content.go 获取。
var (errorType = reflect.TypeOf((*error)(nil)).Elem()fmtStringerType = reflect.TypeOf((*fmt.Stringer)(nil)).Elem()
)// Copied from html/template/content.go.
// indirectToStringerOrError returns the value, after dereferencing as many times
// as necessary to reach the base type (or nil) or an implementation of fmt.Stringer
// or error,
func indirectToStringerOrError(a any) any {if a == nil {return nil}v := reflect.ValueOf(a)for !v.Type().Implements(fmtStringerType) && !v.Type().Implements(errorType) && v.Kind() == reflect.Pointer && !v.IsNil() {v = v.Elem()}return v.Interface()
}
3.To Other Type
那么对其他类型我们也都要实现对应的转换函数。
// ToBoolE casts any type to a bool type.
func ToBoolE(i any) (bool, error) {i = indirect(i)switch b := i.(type) {case bool:return b, nilcase nil:return false, nilcase int:if i.(int) != 0 {return true, nil}return false, nilcase string:return strconv.ParseBool(i.(string))default:return false, fmt.Errorf("unable to cast %#v of type %T to bool", i, i)}
}// ToIntE, ToInt8E, ToInt16E...
3.泛型
最终,我们可以通过泛型完成对上面多个具体类型转换函数的封装。这样我们只需要调用一个函数,便可完成对所有类型的转换。
// ToAnyE converts one type to another and returns an error if occurred.
func ToAnyE[T any](a any) (T, error) {var t Tswitch any(t).(type) {case bool:v, err := ToBoolE(a)if err != nil {return t, err}t = any(v).(T)case int:v, err := ToIntE(a)if err != nil {return t, err}t = any(v).(T)case int8:v, err := ToInt8E(a)if err != nil {return t, err}t = any(v).(T)case int16:v, err := ToInt16E(a)if err != nil {return t, err}t = any(v).(T)case int32:v, err := ToInt32E(a)if err != nil {return t, err}t = any(v).(T)case int64:v, err := ToInt64E(a)if err != nil {return t, err}t = any(v).(T)case uint:v, err := ToUintE(a)if err != nil {return t, err}t = any(v).(T)case uint8:v, err := ToUint8E(a)if err != nil {return t, err}t = any(v).(T)case uint16:v, err := ToUint16E(a)if err != nil {return t, err}t = any(v).(T)case uint32:v, err := ToUint32E(a)if err != nil {return t, err}t = any(v).(T)case uint64:v, err := ToUint64E(a)if err != nil {return t, err}t = any(v).(T)case float32:v, err := ToFloat32E(a)if err != nil {return t, err}t = any(v).(T)case float64:v, err := ToFloat64E(a)if err != nil {return t, err}t = any(v).(T)case string:v, err := ToStringE(a)if err != nil {return t, err}t = any(v).(T)default:return t, fmt.Errorf("the type %T is not supported", t)}return t, nil
}
如果不关心错误,可以再封装一下。
// ToAny converts one type to another type.
func ToAny[T any](a any) T {v, _ := ToAnyE[T](a)return v
}
4.使用示例
package mainimport ("fmt"
)func main() {fmt.Println(ToAny[string](1)) // "1"fmt.Println(ToAny[string](true)) // "true"fmt.Println(ToAny[string](1.1)) // "1.1"fmt.Println(ToAny[int]("1")) // 1fmt.Println(ToAny[int]("1.0")) // 1fmt.Println(ToAny[int](true)) // 1fmt.Println(ToAny[bool]("true")) // truefmt.Println(ToAny[bool]("false")) // falsefmt.Println(ToAny[bool]("True")) // truefmt.Println(ToAny[bool]("False")) // falsefmt.Println(ToAny[bool](1)) // truefmt.Println(ToAny[bool](0)) // falsefmt.Println(ToAny[bool](nil)) // false
}
5.go-huge-util
为了方便大家使用,以上相关代码已开源至 Github 工具库 go-huge-util,大家可使用 go mod 方式 import 然后使用。
import "github.com/dablelv/go-huge-util/conv"conv.ToAny[string](1) // "1"
conv.ToAny[string](true) // "true"
conv.ToAny[string](1.1) // "1.1"conv.ToAny[int]("1") // 1
conv.ToAny[int]("1.0") // 1
conv.ToAny[int](true) // 1conv.ToAny[bool]("true") // true
conv.ToAny[bool]("false") // false
conv.ToAny[bool]("True") // true
conv.ToAny[bool]("False") // false
conv.ToAny[bool](1) // true
conv.ToAny[bool](0) // false
conv.ToAny[bool](nil) // false
go-huge-util 除了类型转换,还有很多其他实用函数,如加解密、zip 等,欢迎大家使用、Star 和 Pull Request。
参考文献
github.com/dablelv/go-huge-util