Documentation
¶
Overview ¶
Package cast provides safe, generic alternatives to Go's standard type assertion.
This package simplifies type assertions by providing convenient, single-expression functions that handle the `value, ok` idiom in different ways, such as returning a default value or a zero value upon failure.
Example:
var myVal any = "hello"
// Standard Go type assertion
str, ok := myVal.(string)
if !ok {
str = "default"
}
// With cast package - cleaner and more concise
str = cast.Or(myVal, "default")
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Or ¶
Or attempts to perform a type assertion of v to type T. If the assertion is successful, it returns the converted value. If the assertion fails, it returns the provided default value `def`.
func OrZero ¶
OrZero attempts to perform a type assertion of v to type T. If the assertion is successful, it returns the converted value. If the assertion fails, it returns the zero value of type T.
Types ¶
This section is empty.