Documentation
¶
Overview ¶
Package cast provides helpful functions for casting. It includes casting between different (castable)types for slice.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func As ¶
As creates new slice instance and casts from elements to result slice. It reports true when from slice is empty.
Example ¶
package main
import (
"fmt"
"github.com/Prastiwar/Go-flow/reflection/cast"
)
func main() {
arr := []interface{}{"1", "2", "3"}
stringArr, ok := cast.As[string](arr)
if !ok {
panic("cannot cast between provided two types")
}
fmt.Println(stringArr)
}
Output: [1 2 3]
func Parse ¶ added in v0.12.0
Parse creates new slice instance and casts, converts or parses from elements to result slice. It reports true when from slice is empty or false if there was any error during parsing.
NOTE: conversion from untyped int to string yields a string of one rune, not a string of digits.
Example ¶
package main
import (
"fmt"
"github.com/Prastiwar/Go-flow/reflection/cast"
)
func main() {
arr := []interface{}{"1", "2", "3"}
stringArr, ok := cast.Parse[int32](arr)
if !ok {
panic("cannot parse between provided two types")
}
fmt.Println(stringArr)
}
Output: [1 2 3]
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.