Documentation
¶
Overview ¶
Package stringfrom provides utilities for converting various Go types to strings with safe handling of nil pointers and customizable default values.
The package includes:
- Ptr, TimePtr, UUIDPtr, IntPtr, UintPtr, Int64Ptr, Float64Ptr and BoolPtr, which dereference a pointer and format the value. For a nil pointer they return their strForNil arguments joined without a separator, or "" if none are passed. TimePtr treats a pointer to the zero time like nil.
- Bool, which returns one of two passed strings.
- Interface, which converts any value, or the value a pointer points to: a value with a Format, Error or String method, also one with a pointer receiver, with that method, a value of string kind as is, other values with fmt.Sprint. It returns "" for nil, a nil pointer, or a nil map, slice, func, channel or interface value.
None of the functions panics for nil input.
Index ¶
- func Bool(boolVal bool, trueString, falseString string) string
- func BoolPtr(ptr *bool, strForNil ...string) string
- func Float64Ptr(ptr *float64, strForNil ...string) string
- func Int64Ptr(ptr *int64, strForNil ...string) string
- func IntPtr(ptr *int, strForNil ...string) string
- func Interface(i any) string
- func Ptr(ptr *string, strForNil ...string) string
- func TimePtr(ptr *time.Time, strForNil ...string) string
- func UUIDPtr(ptr *uu.ID, strForNil ...string) string
- func UintPtr(ptr *uint, strForNil ...string) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BoolPtr ¶
BoolPtr safely dereferences a *bool pointer and returns "true"/"false" string or a custom string for nil.
func Float64Ptr ¶
Float64Ptr safely dereferences a *float64 pointer and returns its string representation or a custom string for nil.
func Int64Ptr ¶
Int64Ptr safely dereferences a *int64 pointer and returns its string representation or a custom string for nil.
func IntPtr ¶
IntPtr safely dereferences a *int pointer and returns its string representation or a custom string for nil.
func Interface ¶
Interface converts i, or the value i points to, to a string, so a non-pointer value returns the same string as a pointer to it. If the value or a pointer to it has a Format, Error or String method, the value is formatted with fmt.Sprint through a pointer to it, so methods with a pointer receiver are used too. Otherwise a value of string kind is returned as is, and other values are formatted with fmt.Sprint. Returns an empty string for nil, a nil pointer, or a nil map, slice, func, channel or interface value.
func Ptr ¶
Ptr safely dereferences a *string pointer and returns the value or a custom string for nil. If ptr is nil, returns the concatenation of strForNil arguments.
func TimePtr ¶
TimePtr safely dereferences a *time.Time pointer and returns its string representation or a custom string for nil/zero values.
Types ¶
This section is empty.