diff --git a/init.go b/init.go index 3455b98..37e40a3 100644 --- a/init.go +++ b/init.go @@ -42,6 +42,8 @@ var ( Console *console // Browser 浏览器操作实例 Browser *browser + // StringConvert 字符串转为任意类型 + StringConvert *stringConvert ) func init() { @@ -70,4 +72,6 @@ func init() { "linux": "xdg-open", }, } + // StringConvert + StringConvert = &stringConvert{} } diff --git a/string_convert.go b/string_convert.go new file mode 100644 index 0000000..b6774f8 --- /dev/null +++ b/string_convert.go @@ -0,0 +1,169 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-05-04 18:25 +package util + +import ( + "github.com/pkg/errors" +) + +type stringConvert struct { +} + +// ToFloat32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:26 2023/5/4 +func (sc *stringConvert) ToFloat32(str string) (float32, error) { + return 0, nil +} + +// ToFloat64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:26 2023/5/4 +func (sc *stringConvert) ToFloat64(str string) (float64, error) { + return 0, nil +} + +// ToDouble 转double +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:33 2023/5/4 +func (sc *stringConvert) ToDouble(str string) (float64, error) { + return sc.ToFloat64(str) +} + +// ToNumber 转数字, 使用最高精度的float64 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:33 2023/5/4 +func (sc *stringConvert) ToNumber(str string) (float64, error) { + return sc.ToFloat64(str) +} + +// ToInt8 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt8(str string) (int8, error) { + return 0, nil +} + +// ToInt16 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt16(str string) (int16, error) { + return 0, nil +} + +// ToInt32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt32(str string) (int32, error) { + return 0, nil +} + +// ToInt64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToInt64(str string) (int64, error) { + return 0, nil +} + +// ToUint8 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:30 2023/5/4 +func (sc *stringConvert) ToUint8(str string) (uint8, error) { + return 0, nil +} + +// ToUint16 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint16(str string) (uint16, error) { + return 0, nil +} + +// ToUint32 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint32(str string) (uint32, error) { + return 0, nil +} + +// ToUint64 ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint64(str string) (uint64, error) { + return 0, nil +} + +// ToUint ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:29 2023/5/4 +func (sc *stringConvert) ToUint(str string) (uint, error) { + return 0, nil +} + +// ToBool ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:34 2023/5/4 +func (sc *stringConvert) ToBool(str string) (bool, error) { + return false, nil +} + +// ToObject ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:35 2023/5/4 +func (sc *stringConvert) ToObject(str string) (map[string]interface{}, error) { + var ( + err error + result map[string]interface{} + ) + if err = JSON.UnmarshalWithNumber([]byte(str), &result); nil != err { + return nil, err + } + return result, nil +} + +// ToStruct ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 18:38 2023/5/4 +func (sc *stringConvert) ToStruct(str string, receiver interface{}) error { + if nil == receiver { + return errors.New("receiver is nil") + } + return JSON.UnmarshalWithNumber([]byte(str), receiver) +}