package mathutilimport ()// ConvOption convert optionstypeConvOption[ any] struct {// if ture: value is nil, will return convert error; // if false(default): value is nil, will convert to zero value NilAsFail bool// HandlePtr auto convert ptr type(int,float,string) value. eg: *int to int // - if true: will use real type try convert. default is false // - NOTE: current T type's ptr is default support. HandlePtr bool// StrictMode for convert value. default is false // // TRUE: // - to int: string, float will return error StrictMode bool// set custom fallback convert func for not supported type. UserConvFn ToTypeFunc[]}// NewConvOption create a new ConvOptionfunc [ any]( ...ConvOptionFn[]) *ConvOption[] { := &ConvOption[]{} .WithOption(...)return}// WithOption set convert optionfunc ( *ConvOption[]) ( ...ConvOptionFn[]) {for , := range {if != nil { () } }}// ConvOptionFn convert option functypeConvOptionFn[ any] func(opt *ConvOption[])// WithNilAsFail set ConvOption.NilAsFail option//// Example://// ToIntWithFunc(val, mathutil.WithNilAsFail[int])func [ any]( *ConvOption[]) { .NilAsFail = true }// WithHandlePtr set ConvOption.HandlePtr optionfunc [ any]( *ConvOption[]) { .HandlePtr = true }// WithStrictMode set ConvOption.StrictMode optionfunc [ any]( *ConvOption[]) { .StrictMode = true }// WithUserConvFn set ConvOption.UserConvFn optionfunc [ any]( ToTypeFunc[]) ConvOptionFn[] {returnfunc( *ConvOption[]) { .UserConvFn = }}/************************************************************* * region Strict to int/uint *************************************************************/// StrictInt check the given value is an integer(intX,uintX), return the int64 value and true if successfunc ( any) (int64, bool) {switch tVal := .(type) {caseint:returnint64(), truecaseint8:returnint64(), truecaseint16:returnint64(), truecaseint32:returnint64(), truecaseint64:return , truecaseuint:returnint64(), truecaseuint8:returnint64(), truecaseuint16:returnint64(), truecaseuint32:returnint64(), truecaseuint64:returnint64(), truecaseuintptr:returnint64(), truedefault:return0, false }}// StrictUint strict check value is integer(intX,uintX) and convert to uint64.func ( any) (uint64, bool) {switch tVal := .(type) {caseint:returnuint64(), truecaseint8:returnuint64(), truecaseint16:returnuint64(), truecaseint32:returnuint64(), truecaseint64:returnuint64(), truecaseuint:returnuint64(), truecaseuint8:returnuint64(), truecaseuint16:returnuint64(), truecaseuint32:returnuint64(), truecaseuint64:return , truecaseuintptr:returnuint64(), truedefault:return0, false }}/************************************************************* * region convert to float64 *************************************************************/// QuietFloat convert value to float64, will ignore error. alias of SafeFloatfunc ( any) float64 { returnSafeFloat() }// SafeFloat convert value to float64, will ignore errorfunc ( any) float64 { , := ToFloatWith()return}// FloatOrPanic convert value to float64, will panic on errorfunc ( any) float64 { returnMustFloat() }// MustFloat convert value to float64, will panic on errorfunc ( any) float64 { , := ToFloatWith()if != nil {panic() }return}// FloatOrDefault convert value to float64, will return default value on errorfunc ( any, float64) float64 { returnFloatOr(, ) }// FloatOr convert value to float64, will return default value on errorfunc ( any, float64) float64 { , := ToFloatWith()if != nil {return }return}// Float convert value to float64, return error on failedfunc ( any) (float64, error) { returnToFloatWith() }// FloatOrErr convert value to float64, return error on failedfunc ( any) (float64, error) { returnToFloatWith() }// ToFloat convert value to float64, return error on failedfunc ( any) (float64, error) { returnToFloatWith() }// ToFloatWith try to convert value to float64. can with some option func, more see ConvOption.func ( any, ...ConvOptionFn[float64]) ( float64, error) { := NewConvOption(...)if !.NilAsFail && == nil {return0, nil }switch tVal := .(type) {casestring: , = strconv.ParseFloat(strings.TrimSpace(), 64)caseint: = float64()caseint8: = float64()caseint16: = float64()caseint32: = float64()caseint64: = float64()caseuint: = float64()caseuint8: = float64()caseuint16: = float64()caseuint32: = float64()caseuint64: = float64()casefloat32: = float64()casefloat64: = case *float64: // default support float64 ptr type = *casetime.Duration: = float64()casecomdef.Float64able: // eg: json.Number , = .Float64()default:if .HandlePtr {if := reflect.ValueOf(); .Kind() == reflect.Pointer { = .Elem()ifcheckfn.IsSimpleKind(.Kind()) {return (.Interface(), ...) } } }if .UserConvFn != nil { , = .UserConvFn() } else { = comdef.ErrConvType } }return}/************************************************************* * region intX/floatX to string *************************************************************/// MustString convert intX/floatX value to string, will panic on errorfunc ( any) string { , := ToStringWith()if != nil {panic() }return}// StringOrPanic convert intX/floatX value to string, will panic on errorfunc ( any) string { returnMustString() }// StringOrDefault convert intX/floatX value to string, will return default value on errorfunc ( any, string) string { returnStringOr(, ) }// StringOr convert intX/floatX value to string, will return default value on errorfunc ( any, string) string { , := ToStringWith()if != nil {return }return}// ToString convert intX/floatX value to string, return error on failedfunc ( any) (string, error) { returnToStringWith() }// StringOrErr convert intX/floatX value to string, return error on failedfunc ( any) (string, error) { returnToStringWith() }// QuietString convert intX/floatX value to string, other type convert by fmt.Sprintfunc ( any) string { returnSafeString() }// String convert intX/floatX value to string, other type convert by fmt.Sprintfunc ( any) string { , := TryToString(, false)return}// SafeString convert intX/floatX value to string, other type convert by fmt.Sprintfunc ( any) string { , := TryToString(, false)return}// TryToString try convert intX/floatX value to string//// if defaultAsErr is False, will use fmt.Sprint convert other typefunc ( any, bool) (string, error) {varcomfunc.ConvOptionFnif ! { = comfunc.WithUserConvFn(comfunc.StrBySprintFn) }returnToStringWith(, )}// ToStringWith try to convert value to string. can with some option func, more see comfunc.ConvOption.func ( any, ...comfunc.ConvOptionFn) (string, error) {returncomfunc.ToStringWith(, ...)}
The pages are generated with Goldsv0.8.4. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.