// 将字符串转换成其它基本类型 package cast import ( "fmt" "reflect" "strconv" "strings" ) const message = "cast: cannot cast `%v` to type `%v`" func Uint(value string) (uint, error) { v, err := strconv.ParseUint(value, 0, strconv.IntSize) if err != nil { return 0, err } return uint(v), nil } func Uint8(value string) (uint8, error) { v, err := strconv.ParseUint(value, 0, 8) if err != nil { return 0, err } return uint8(v), nil } func Uint16(value string) (uint16, error) { v, err := strconv.ParseUint(value, 0, 16) if err != nil { return 0, err } return uint16(v), nil } func Uint32(value string) (uint32, error) { v, err := strconv.ParseUint(value, 0, 32) if err != nil { return 0, err } return uint32(v), nil } func AsUint64(value string) (uint64, error) { v, err := strconv.ParseUint(value, 0, 64) if err != nil { return 0, err } return v, nil } func Int(value string) (int, error) { v, err := strconv.ParseInt(value, 0, strconv.IntSize) if err != nil { return 0, fmt.Errorf(message, value, "int") } return int(v), nil } func Int8(value string) (int8, error) { v, err := strconv.ParseInt(value, 0, 8) if err != nil { return 0, err } return int8(v), nil } func Int16(value string) (int16, error) { v, err := strconv.ParseInt(value, 0, 16) if err != nil { return 0, err } return int16(v), nil } func Int32(value string) (int32, error) { v, err := strconv.ParseInt(value, 0, 32) if err != nil { return 0, err } return int32(v), nil } func Int64(value string) (int64, error) { v, err := strconv.ParseInt(value, 0, 64) if err != nil { return 0, err } return v, nil } func Float32(value string) (float32, error) { v, err := strconv.ParseFloat(value, 64) if err != nil { return 0, err } return float32(v), nil } func Float64(value string) (float64, error) { v, err := strconv.ParseFloat(value, 64) if err != nil { return 0, err } return v, nil } func Bool(value string) (bool, error) { v, err := strconv.ParseBool(value) if err != nil { return false, err } return v, nil } // FromType casts a string value to the given reflected type. func FromType(value string, targetType reflect.Type) (interface{}, error) { var typeName = targetType.String() if strings.HasPrefix(typeName, "[]") { itemType := typeName[2:] array := reflect.New(targetType).Elem() for _, v := range strings.Split(value, ",") { if item, err := FromString(strings.Trim(v, " \n\r"), itemType); err != nil { return array.Interface(), err } else { array = reflect.Append(array, reflect.ValueOf(item)) } } return array.Interface(), nil } return FromString(value, typeName) } // FromString casts a string value to the given type name. func FromString(value string, targetType string) (any, error) { switch targetType { case "int": return Int(value) case "int8": return Int8(value) case "int16": return Int16(value) case "int32": return Int32(value) case "int64": return Int64(value) case "uint": return Uint(value) case "uint8": return Uint8(value) case "uint16": return Uint16(value) case "uint32": return Uint32(value) case "uint64": return AsUint64(value) case "bool": return Bool(value) case "float32": return Float32(value) case "float64": return Float64(value) case "string": return value, nil } return nil, fmt.Errorf("cast: type %v is not supported", targetType) }