Documentation
¶
Overview ¶
Package tc provides simple functions for type converting, casting, assigning with default and comparing.
Index ¶
- func Cmp[T any](c bool, t T, f T) T
- func CmpN[T any](c *bool, t T, f T, n T) T
- func DefCast[T any](x any, def T) T
- func DefPx[T any](x *T, def T) T
- func DefZero[T comparable](x T, def T) T
- func P[T any](x T) *T
- func Pn[T comparable](x T) *T
- func SafeCast[T any](x any) T
- func SafePx[T any](x *T) T
- func Zero[T comparable](x T) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cmp ¶
Cmp is sort of ternary operator. Returns t or f as c is true or false.
Example ¶
Config.DisablePointerAddresses = true Dump(Cmp(true, "T", "F")) // "T" — sort of ternary operator Dump(Cmp(false, "T", "F")) // "F"
Output: (string) (len=1) "T" (string) (len=1) "F"
func CmpN ¶
CmpN is sort of ternary operator, however it is considering pointer to bool, including nil value. Returns t or f as c is true or false. Or n if nil.
Example ¶
Config.DisablePointerAddresses = true t := P(true) // pointer to true f := P(false) // pointer to false Dump(CmpN(t, "T", "F", "N")) // "T" — sort of ternary operator Dump(CmpN(f, "T", "F", "N")) // "F" Dump(CmpN((*bool)(nil), "T", "F", "N")) // "N"
Output: (string) (len=1) "T" (string) (len=1) "F" (string) (len=1) "N"
func DefCast ¶
DefCast casts any to T or return def.
Example ¶
Config.DisablePointerAddresses = true Dump(DefCast(any("X"), "Y")) // "X" — cast any to type T Dump(DefCast(any(1), "Y")) // "Y" — def Dump(DefCast(any(""), "Y")) // "" — cast zero value to itself
Output: (string) (len=1) "X" (string) (len=1) "Y" (string) ""
func DefPx ¶
func DefPx[T any](x *T, def T) T
DefPx denotes the pointer's underlying value or return def if pointer is nil.
Example ¶
Config.DisablePointerAddresses = true p := P(1) // pointer to 1 Dump(DefPx(p, 3)) // 1 — &1 -> 1 Dump(DefPx((*int)(nil), 3)) // 3 — &nil -> def=3
Output: (int) 1 (int) 3
func DefZero ¶
func DefZero[T comparable](x T, def T) T
DefZero returns def if argument has zero value.
Example ¶
Config.DisablePointerAddresses = true Dump(DefZero("X", "D")) // "X" Dump(DefZero("", "D")) // "D"
Output: (string) (len=1) "X" (string) (len=1) "D"
func P ¶
func P[T any](x T) *T
P simply returns pointer to any type T.
Example ¶
Config.DisablePointerAddresses = true Dump(P(1)) // (*int)(1) — just take pointer Dump(P(0)) // (*int)(0) — zero value as is
Output: (*int)(1) (*int)(0)
func Pn ¶
func Pn[T comparable](x T) *T
Pn returns pointer to T or nil pointer for zero value of T.
Example ¶
Config.DisablePointerAddresses = true Dump(Pn(1)) // (*int)(1) — smart pointer with Dump(Pn(0)) // (*int)(<nil>) — zero values to nil pointer
Output: (*int)(1) (*int)(<nil>)
func SafeCast ¶
SafeCast casts any to T or return zero value of T.
Example ¶
Config.DisablePointerAddresses = true Dump(SafeCast[string](any("X"))) // "X" — cast any to type T Dump(SafeCast[string](any(1))) // "" — cast to zero value on error
Output: (string) (len=1) "X" (string) ""
func SafePx ¶
func SafePx[T any](x *T) T
SafePx denotes the pointer's underlying value or return zero values of T for nil pointers.
Example ¶
Config.DisablePointerAddresses = true p := P(1) // pointer to 1 Dump(SafePx(p)) // 1 — &1 -> 1 Dump(SafePx((*int)(nil))) // 0 — &nil -> 0 (zero value)
Output: (int) 1 (int) 0
func Zero ¶
func Zero[T comparable](x T) bool
Zero turns true if x has zero value.
Example ¶
Config.DisablePointerAddresses = true Dump(Zero("X")) // false Dump(Zero("")) // true — zero value
Output: (bool) false (bool) true
Types ¶
This section is empty.