Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( Error = errors.Errorf("unable to cast value") ErrorSignedToUnsigned = errors.Wrap(Error, "cannot cast signed value to unsigned integer") ErrorInvalidOption = "invalid %s value '%v'" ErrorStrErrorCastingFunc = "error casting %T to %T during function generation" ErrorStrUnableToCast = "unable to cast %#.10v of type %T to %T" )
Functions ¶
func To ¶
To casts the value v to the given type, ignoring any errors. See the ToE documentation more information.
Example (Float64) ¶
v := cast.To[float64]("1.234")
fmt.Printf("%#v (%T)", v, v)
Output: 1.234 (float64)
Example (Int) ¶
v := cast.To[int]("1")
fmt.Printf("%#v (%T)", v, v)
Output: 1 (int)
Example (String) ¶
v := cast.To[string](1.234)
fmt.Printf("%#v (%T)", v, v)
Output: "1.234" (string)
func ToE ¶
ToE casts the value v to the given type, returning any errors.
o (Ops) is an optional parameter providing flags that can be used to modify the default type conversion behavior. If ops is not provided, the default conversion behavior for a given type is used. Available options depend on the target type, see the documentation for the specific type conversion function for more information.
Complex types have specific default behaviors, for example:
If the target type is a channel, a channel with a buffer of 1 is created and the cast value `v` is added to the the channel before it is returned.
If the target type is an array a slice is created. To create a slice with a backing array with a spcific size, set the LENGTH flag to the desired size as an integer: `slice, err := cast.ToE[[]int](v, Ops{LENGTH: 10})`. The value `v` is cast to the required type and appended to the returned slice.
If the target type is a map, a map is created with a zero-value key containing the cast value `v` which is then returned.
See the documentation for the specific type conversion function for more information.
Example (Error_with_default) ¶
v, e := cast.ToE[int]("Hi!", cast.Op{cast.DEFAULT, 10})
fmt.Printf("%#v (%T), %v", v, v, e)
Output: 10 (int), unable to cast "Hi!" of type string to int
Example (Float64) ¶
v, e := cast.ToE[float64]("1")
fmt.Printf("%#v (%T), %v", v, v, e)
Output: 1 (float64), <nil>
Example (Int) ¶
v, e := cast.ToE[int]("1")
fmt.Printf("%#v (%T), %v", v, v, e)
Output: 1 (int), <nil>
Example (String) ¶
v, e := cast.ToE[string](float64(1.0))
fmt.Printf("%#v (%T), %v", v, v, e)
Output: "1" (string), <nil>
Example (Uint_abs) ¶
v, e := cast.ToE[uint]("-1", cast.Op{cast.ABS, true})
fmt.Printf("%v (%T), %v", v, v, e)
Output: 1 (uint), <nil>
Example (Uint_err) ¶
v, e := cast.ToE[uint]("-1")
fmt.Printf("%v (%T), %v", v, v, e)
Output: 0 (uint), unable to cast "-1" of type string to uint
Types ¶
type Flag ¶ added in v2.0.1
type Flag int
const ( DEFAULT Flag = iota // TTo, default: TTo zero value, value to return on error ABS // bool, default: false, use absolute value during uint conversion DUPLICATE_KEY_ERROR // bool, default: false, error on duplicate map key LENGTH // int, default: 1, number of elements in result UNIQUE_VALUES // bool, default: false, dedupe slice values JSON // bool, default: false, encode strings as JSON )
type Tchan ¶
type Tchan interface {
~chan Tbase |
~chan []int | ~chan []int8 | ~chan []int16 | ~chan []int32 | ~chan []int64 |
~chan []uint | ~chan []uint8 | ~chan []uint16 | ~chan []uint32 | ~chan []uint64 | ~chan []uintptr |
~chan []float32 | ~chan []float64 |
~chan []complex64 | ~chan []complex128 |
~chan []string | ~chan []bool |
~chan []any | ~chan Func[Tbase]
}
type Tmap ¶
type Tmap interface {
~map[Tbase]Tbase |
~map[Tbase][]int |
~map[Tbase][]int8 |
~map[Tbase][]int16 |
~map[Tbase][]int32 |
~map[Tbase][]int64 |
~map[Tbase][]uint |
~map[Tbase][]uint8 |
~map[Tbase][]uint16 |
~map[Tbase][]uint32 |
~map[Tbase][]uint64 |
~map[Tbase][]uintptr |
~map[Tbase][]float32 |
~map[Tbase][]float64 |
~map[Tbase][]complex64 |
~map[Tbase][]complex128 |
~map[Tbase][]string |
~map[Tbase][]bool |
~map[Tbase][]any |
~map[Tbase][]Func[Tbase]
}
