Documentation
¶
Overview ¶
Package gconv helps users convert any type to the convertible type.
Example ¶
fmt.Println(To[string](1)) // "1"
fmt.Println(To[int]("1")) // 1
fmt.Println(To[int]("x")) // 0
fmt.Println(To[bool]("true")) // true
fmt.Println(To[bool]("x")) // false
fmt.Println(To[int](gptr.Of(gptr.Of(gptr.Of("1"))))) // 1
type myInt int
type myString string
fmt.Println(To[myInt](myString("1"))) // 1
fmt.Println(To[myString](myInt(1))) // "1"
fmt.Println(ToE[int]("x")) // 0 strconv.ParseInt: parsing "x": invalid syntax
fmt.Println(ToE[int]("1.10")) // 0 strconv.ParseInt: parsing "1.1": invalid syntax
fmt.Println(ToE[int]("1.00")) // 1 nil
fmt.Println(ToE[float64](".1")) // 0.1 nil
Output: 1 1 0 true false 1 1 1 0 strconv.ParseInt: parsing "x": invalid syntax 0 strconv.ParseInt: parsing "1.1": invalid syntax 1 <nil> 0.1 <nil>
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func To ¶
func To[T convertible, V any](v V) T
To converts any type to the convertible type. If the conversion is not supported, a zero value is returned.
🚀 EXAMPLE:
To[bool]("true") ⏩ true
To[int64]("1.0") ⏩ 1
To[string](true) ⏩ "true"
p := gptr.Of(gptr.Of(gptr.Of('a')))
To[int64](p) ⏩ 97
To[int64]("a") ⏩ 0
⚠️ WARNING: byte is an alias for uint8, rune is an alias for int32.
To[string]('a') ⏩ "97"
To[string]('中') ⏩ "20013"
func ToE ¶
ToE converts any type to the convertible type. If the conversion is not supported, a zero value and an error is returned.
🚀 EXAMPLE:
ToE[bool]("true") ⏩ true, nil
ToE[int64]("1.0") ⏩ 1, nil
ToE[string](true) ⏩ "true", nil
p := gptr.Of(gptr.Of(gptr.Of('a')))
ToE[int64](p) ⏩ 97, nil
ToE[int64]("a") ⏩ 0, "strconv.ParseInt: parsing \"a\": invalid syntax"
⚠️ WARNING: byte is an alias for uint8, rune is an alias for int32.
ToE[string]('a') ⏩ "97", nil
ToE[string]('中') ⏩ "20013", nil
func ToPtr ¶
func ToPtr[T convertible, V any](v V) *T
ToPtr converts any type to a pointer of the convertible type. If the conversion is not supported, nil is returned.
🚀 EXAMPLE:
ToPtr[bool]("true") ⏩ (*bool)(true)
ToPtr[int64]("1.0") ⏩ (*int64)(1)
ToPtr[string](true) ⏩ (*string)("true")
p := gptr.Of(gptr.Of(gptr.Of('a')))
ToPtr[int64](p) ⏩ (*int64)(97)
ToPtr[int64]("a") ⏩ (*int64)(nil)
⚠️ WARNING: byte is an alias for uint8, rune is an alias for int32.
ToPtr[string]('a') ⏩ (*string)("97")
ToPtr[string]('中') ⏩ (*string)("20013")
func ToR ¶
ToR converts any type to gresult.R.
🚀 EXAMPLE:
ToR[bool]("true") ⏩ gresult.OK(true)
ToR[int64]("1.0") ⏩ gresult.OK[int64](1)
ToR[string](true) ⏩ gresult.OK("true")
p := gptr.Of(gptr.Of(gptr.Of('a')))
ToR[int64](p) ⏩ gresult.OK[int64](97)
ToR[int64]("a") ⏩ gresult.Err[int64]("strconv.ParseInt: parsing \"a\": invalid syntax")
⚠️ WARNING: byte is an alias for uint8, rune is an alias for int32.
ToR[string]('a') ⏩ gresult.OK("97")
ToR[string]('中') ⏩ gresult.OK("20013")
Types ¶
This section is empty.