增加string包装类型

This commit is contained in:
白茶清欢 2023-05-05 13:43:33 +08:00
commit cb65c45739
5 changed files with 750 additions and 0 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
.idea
.vscode
release
logs

19
go.mod Normal file
View File

@ -0,0 +1,19 @@
module git.zhangdeman.cn/zhangdeman/wrapper
go 1.20
require (
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230505025924-96532aff0019 // indirect
github.com/Jeffail/gabs v1.4.0 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mssola/user_agent v0.6.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

28
go.sum Normal file
View File

@ -0,0 +1,28 @@
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230505025924-96532aff0019 h1:eJ/9rEj2iI8P9I1DfCmMUvsV+n2EiAWCXnI9yVVDHO0=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230505025924-96532aff0019/go.mod h1:z2bP5LIwRVpWSQV0/a3WIFaoarJUP8kA/0Clv0bP+8I=
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4=
github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

657
string.go Normal file
View File

@ -0,0 +1,657 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 11:59
package wrapper
import (
"errors"
"git.zhangdeman.cn/zhangdeman/util"
"strings"
)
// String 字符串类型包装
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 12:07 2023/5/5
type String string
// ToFloat32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:26 2023/5/4
func (str String) ToFloat32() (float32, error) {
var (
err error
res float32
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToFloat64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:26 2023/5/4
func (str String) ToFloat64() (float64, error) {
var (
err error
res float64
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToDouble 转double
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:33 2023/5/4
func (str String) ToDouble() (float64, error) {
return str.ToFloat64()
}
// ToNumber 转数字, 使用最高精度的float64
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:33 2023/5/4
func (str String) ToNumber() (float64, error) {
return str.ToFloat64()
}
// ToInt8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToInt8() (int8, error) {
var (
err error
res int8
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToInt16 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToInt16() (int16, error) {
var (
err error
res int16
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToInt32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToInt32() (int32, error) {
var (
err error
res int32
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToInt64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToInt64() (int64, error) {
var (
err error
res int64
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToInt ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:47 2023/5/5
func (str String) ToInt() (int, error) {
var (
err error
res int
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToUint8 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:30 2023/5/4
func (str String) ToUint8() (uint8, error) {
var (
err error
res uint8
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToUint16 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToUint16() (uint16, error) {
var (
err error
res uint16
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToUint32 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToUint32() (uint32, error) {
var (
err error
res uint32
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToUint64 ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToUint64() (uint64, error) {
var (
err error
res uint64
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToUint ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:29 2023/5/4
func (str String) ToUint() (uint, error) {
var (
err error
res uint
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToBool ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:34 2023/5/4
func (str String) ToBool() (bool, error) {
var (
err error
res bool
)
err = util.ConvertAssign(&res, str)
return res, err
}
// ToObject ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:35 2023/5/4
func (str String) ToObject() (map[string]interface{}, error) {
var (
err error
result map[string]interface{}
)
if err = util.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 (str String) ToStruct(receiver interface{}) error {
if nil == receiver {
return errors.New("receiver is nil")
}
return util.JSON.UnmarshalWithNumber([]byte(str), receiver)
}
// ToInt8Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:55 2023/5/4
func (str String) ToInt8Slice(splitChar ...string) ([]int8, error) {
var (
err error
res []int8
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int8, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToInt8(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToInt16Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:01 2023/5/4
func (str String) ToInt16Slice(splitChar ...string) ([]int16, error) {
var (
err error
res []int16
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int16, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToInt16(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToInt32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:03 2023/5/4
func (str String) ToInt32Slice(splitChar ...string) ([]int32, error) {
var (
err error
res []int32
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int32, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToInt32(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToInt64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:04 2023/5/4
func (str String) ToInt64Slice(splitChar ...string) ([]int64, error) {
var (
err error
res []int64
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int64, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToInt64(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToIntSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 19:04 2023/5/4
func (str String) ToIntSlice(splitChar ...string) ([]int, error) {
var (
err error
res []int
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]int, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToInt(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUint8Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:50 2023/5/5
func (str String) ToUint8Slice(splitChar ...string) ([]uint8, error) {
var (
err error
res []uint8
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint8, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToUint8(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUint16Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:52 2023/5/5
func (str String) ToUint16Slice(splitChar ...string) ([]uint16, error) {
var (
err error
res []uint16
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint16, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToUint16(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUint32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:53 2023/5/5
func (str String) ToUint32Slice(splitChar ...string) ([]uint32, error) {
var (
err error
res []uint32
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint32, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToUint32(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUint64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/5
func (str String) ToUint64Slice(splitChar ...string) ([]uint64, error) {
var (
err error
res []uint64
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint64, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToUint64(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToUintSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:54 2023/5/5
func (str String) ToUintSlice(splitChar ...string) ([]uint, error) {
var (
err error
res []uint
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]uint, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToUint(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToBoolSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:55 2023/5/5
func (str String) ToBoolSlice(splitChar ...string) ([]bool, error) {
var (
err error
res []bool
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]bool, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToBool(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToFloat32Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:56 2023/5/5
func (str String) ToFloat32Slice(splitChar ...string) ([]float32, error) {
var (
err error
res []float32
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]float32, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToFloat32(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToFloat64Slice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:56 2023/5/5
func (str String) ToFloat64Slice(splitChar ...string) ([]float64, error) {
var (
err error
res []float64
)
isSplit := len(splitChar) > 0
if !isSplit {
if err = util.JSON.UnmarshalWithNumber([]byte(str), &res); nil != err {
return nil, err
}
return res, nil
}
res = make([]float64, 0)
arr := strings.Split(string(str), splitChar[0])
for _, item := range arr {
if itemVal, err := String(item).ToFloat64(); nil != err {
return nil, err
} else {
res = append(res, itemVal)
}
}
return res, nil
}
// ToDoubleSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/5
func (str String) ToDoubleSlice(splitChar ...string) ([]float64, error) {
return str.ToFloat64Slice(splitChar...)
}
// ToNumberSlice ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:57 2023/5/5
func (str String) ToNumberSlice(splitChar ...string) ([]float64, error) {
return str.ToFloat64Slice(splitChar...)
}

25
string_test.go Normal file
View File

@ -0,0 +1,25 @@
// Package wrapper ...
//
// Description : wrapper ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2023-05-05 13:39
package wrapper
import (
"fmt"
"testing"
)
func TestString_ToFloat32(t *testing.T) {
var str String
str = "12345.123"
fmt.Println(str)
fmt.Println(str.ToFloat32())
fmt.Println(str.ToFloat64())
fmt.Println(str.ToNumber())
fmt.Println(str.ToDouble())
fmt.Println(str.ToInt())
fmt.Println(str.ToUint())
}