Documentation
¶
Overview ¶
Package to provides a comprehensive set of utility functions for type conversion and transformation in Go applications.
The goal of this package is to offer useful, common converters and mappers that can be easily integrated into functional programming patterns, such as being used as mapper functions with the seq package.
These utilities are designed to reduce boilerplate code and provide a consistent API for common transformation operations while maintaining type safety through generics.
Index ¶
- Variables
- func Any[T any](value T) any
- func AtLeast[T types.Ordered](min T) func(value T) T
- func AtLeastThe[T types.Ordered](min T) func(value T) T
- func AtMost[T types.Ordered](max T) func(value T) T
- func AtMostThe[T types.Ordered](max T) func(value T) T
- func BoolFromNumber[V types.Number](value V) bool
- func BoolFromString(value string) (bool, error)
- func CamelCase(str string) string
- func Capitalized(str string) string
- func Ellipsis(str string, length int) string
- func EllipsisWith(length int) func(str string) string
- func EmptyValue[T any]() T
- func Enum[V ~string, T ~string](value V, enumValues ...T) (T, error)
- func EnumStrict[V ~string, T ~string](value V, enumValues ...T) (T, error)
- func Float32[V ConvertableToNumber](value V) (float32, error)
- func Float32FromBool(value bool) float32
- func Float32FromSigned[V types.SignedNumber](value V) (float32, error)
- func Float32FromString(value string) (float32, error)
- func Float32FromUnsigned[V types.Unsigned](value V) (float32, error)
- func Float64[V ConvertableToNumber](value V) (float64, error)
- func Float64FromBool(value bool) float64
- func Float64FromSigned[V types.SignedNumber](value V) (float64, error)
- func Float64FromString(value string) (float64, error)
- func Float64FromUnsigned[V types.Unsigned](value V) (float64, error)
- func Int[V ConvertableToNumber](value V) (int, error)
- func Int16[V ConvertableToNumber](value V) (int16, error)
- func Int16FromBool(value bool) int16
- func Int16FromSigned[V types.SignedNumber](value V) (int16, error)
- func Int16FromString(value string) (int16, error)
- func Int16FromUnsigned[V types.Unsigned](value V) (int16, error)
- func Int32[V ConvertableToNumber](value V) (int32, error)
- func Int32FromBool(value bool) int32
- func Int32FromSigned[V types.SignedNumber](value V) (int32, error)
- func Int32FromString(value string) (int32, error)
- func Int32FromUnsigned[V types.Unsigned](value V) (int32, error)
- func Int64[V ConvertableToNumber](value V) (int64, error)
- func Int64FromBool(value bool) int64
- func Int64FromSigned[V types.SignedNumber](value V) (int64, error)
- func Int64FromString(value string) (int64, error)
- func Int64FromUnsigned[V types.Unsigned](value V) (int64, error)
- func Int8[V ConvertableToNumber](value V) (int8, error)
- func Int8FromBool(value bool) int8
- func Int8FromSigned[V types.SignedNumber](value V) (int8, error)
- func Int8FromString(value string) (int8, error)
- func Int8FromUnsigned[V types.Unsigned](value V) (int8, error)
- func IntFromBool(value bool) int
- func IntFromSigned[V types.SignedNumber](value V) (int, error)
- func IntFromString(value string) (int, error)
- func IntFromUnsigned[V types.Unsigned](value V) (int, error)
- func KebabCase(str string) string
- func Nil[T any]() *T
- func NilOfType[T any](_ *T) *T
- func NoLessThan[T types.Ordered](value, min T) T
- func NoMoreThan[T types.Ordered](value, max T) T
- func Options[T any](opts ...func(*T)) T
- func OptionsWithDefault[T any](defaultOptions T, opts ...func(*T)) T
- func PascalCase(str string) string
- func Ptr[T any](x T) *T
- func Sentences(str string) []string
- func SliceOfAny[T any](collection []T) []any
- func SliceOfPtr[T any](collection []T) []*T
- func SliceOfValue[T any](collection []*T) []T
- func SnakeCase(str string) string
- func String[T any](value T) string
- func StringFromBool(value bool) string
- func StringFromBytes(value []byte) string
- func StringFromFloat[V types.Float](value V) string
- func StringFromInteger[V types.Integer](value V) string
- func StringFromRune(value rune) string
- func UInt[V ConvertableToNumber](value V) (uint, error)
- func UInt16[V ConvertableToNumber](value V) (uint16, error)
- func UInt16FromBool(value bool) uint16
- func UInt16FromNumber[V types.Number](value V) (uint16, error)
- func UInt16FromString(value string) (uint16, error)
- func UInt32[V ConvertableToNumber](value V) (uint32, error)
- func UInt32FromBool(value bool) uint32
- func UInt32FromNumber[V types.Number](value V) (uint32, error)
- func UInt32FromString(value string) (uint32, error)
- func UInt64[V ConvertableToNumber](value V) (uint64, error)
- func UInt64FromBool(value bool) uint64
- func UInt64FromNumber[V types.Number](value V) (uint64, error)
- func UInt64FromString(value string) (uint64, error)
- func UInt8[V ConvertableToNumber](value V) (uint8, error)
- func UInt8FromBool(value bool) uint8
- func UInt8FromNumber[V types.Number](value V) (uint8, error)
- func UInt8FromString(value string) (uint8, error)
- func UIntFromBool(value bool) uint
- func UIntFromNumber[V types.Number](value V) (uint, error)
- func UIntFromString(value string) (uint, error)
- func Value[T any](x *T) T
- func ValueAtLeast[T types.Ordered](value, min T) T
- func ValueAtMost[T types.Ordered](value, max T) T
- func ValueBetween[T types.Ordered](value, min, max T) T
- func ValueBetweenThe[T types.Ordered](min, max T) func(value T) T
- func ValueOr[T any](x *T, fallback T) T
- func ValueOrGet[T any](x *T, fallback func() T) T
- func Words(str string) []string
- func YesNo[T comparable](value T) string
- func ZeroValue[T any]() T
- type ConvertableToNumber
- type IfElseCondition
- func (c *IfElseCondition[T]) Else(resultProvider func() T) T
- func (c *IfElseCondition[T]) ElseIf(condition bool, resultProvider func() T) *IfElseCondition[T]
- func (c *IfElseCondition[T]) ElseIfThen(condition bool, resultWhenTrue T) *IfElseCondition[T]
- func (c *IfElseCondition[T]) ElseThen(resultWhenFalse T) T
- type SwitchCase
- type SwitchThen
Examples ¶
- Any
- AtLeastThe
- AtMostThe
- BoolFromNumber
- BoolFromString
- CamelCase
- Capitalized
- Ellipsis
- EllipsisWith
- EmptyValue
- Enum
- EnumStrict
- Float32
- Float32FromBool
- Float32FromSigned
- Float32FromString
- Float32FromUnsigned
- Float64
- Float64FromBool
- Float64FromSigned
- Float64FromString
- Float64FromUnsigned
- If
- IfElseCondition.ElseIf
- IfElseCondition.ElseIfThen
- IfThen
- Int
- Int16
- Int16FromBool
- Int16FromSigned
- Int16FromString
- Int16FromUnsigned
- Int32
- Int32FromBool
- Int32FromSigned
- Int32FromString
- Int32FromUnsigned
- Int64
- Int64FromBool
- Int64FromSigned
- Int64FromString
- Int64FromUnsigned
- Int8
- Int8FromBool
- Int8FromSigned
- Int8FromString
- Int8FromUnsigned
- IntFromBool
- IntFromSigned
- IntFromString
- IntFromUnsigned
- KebabCase
- Nil
- NilOfType
- Options
- OptionsWithDefault
- PascalCase
- Ptr
- Sentences
- SliceOfAny
- SliceOfPtr
- SliceOfValue
- SnakeCase
- String
- StringFromBool
- StringFromBytes
- StringFromFloat
- StringFromInteger
- StringFromRune
- Switch
- UInt
- UInt16
- UInt16FromBool
- UInt16FromNumber
- UInt16FromString
- UInt32
- UInt32FromBool
- UInt32FromNumber
- UInt32FromString
- UInt64
- UInt64FromBool
- UInt64FromNumber
- UInt64FromString
- UInt8
- UInt8FromBool
- UInt8FromNumber
- UInt8FromString
- UIntFromBool
- UIntFromNumber
- UIntFromString
- Value
- ValueAtLeast
- ValueAtMost
- ValueBetween
- ValueBetweenThe
- ValueOr
- ValueOrGet
- Words
- YesNo
- ZeroValue
Constants ¶
This section is empty.
Variables ¶
var ErrInvalidStringSyntax = strconv.ErrSyntax
ErrInvalidStringSyntax is returned when the string has invalid syntax for conversion to target type.
var ErrValueOutOfRange = fmt.Errorf("%w to convert", strconv.ErrRange)
ErrValueOutOfRange is returned when the value is out of range of the target type.
Functions ¶
func Any ¶
Any casts the value to an any type.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Convert a typed value to any
val := to.Any(42)
fmt.Printf("Type: %T\n", val)
// Convert a string to any
strVal := to.Any("hello")
fmt.Printf("Type: %T\n", strVal)
}
Output: Type: int Type: string
func AtLeast ¶
AtLeast will return a function that will clamp the value to be at least the min value. It's wrapped around NoLessThan function, to make it usable in Map functions.
See Also: NoLessThan @Deprecated: In the future will replace ValueAtLeast, use AtLeastThe instead.
func AtLeastThe ¶ added in v1.2.0
AtLeastThe will return a function that will clamp the value to be at least the min value. It's wrapped around ValueAtLeast function, to make it usable in Map functions.
See Also: ValueAtLeast
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Creating a function that ensures values are at least 18
ensureAdult := to.AtLeastThe(18)
fmt.Printf("ensureAdult(15) = %d\n", ensureAdult(15))
fmt.Printf("ensureAdult(21) = %d\n", ensureAdult(21))
}
Output: ensureAdult(15) = 18 ensureAdult(21) = 21
func AtMost ¶
AtMost will return a function that will clamp the value to be at most the max value. It's wrapped around ValueAtMost function, to make it usable in Map functions.
See Also: ValueAtMost @Deprecated: In the future will replace ValueAtMost, use AtMostThe instead.
func AtMostThe ¶ added in v1.2.0
AtMostThe will return a function that will clamp the value to be at most the max value. It's wrapped around ValueAtMost function, to make it usable in Map functions.
See Also: ValueAtMost
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Creating a function that ensures values are at most 100
ensurePercentage := to.AtMostThe(100)
fmt.Printf("ensurePercentage(50) = %d\n", ensurePercentage(50))
fmt.Printf("ensurePercentage(150) = %d\n", ensurePercentage(150))
}
Output: ensurePercentage(50) = 50 ensurePercentage(150) = 100
func BoolFromNumber ¶
BoolFromNumber will convert any number to bool 0 is false, any other number is true.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting various numeric values to bool
fmt.Println("0 ->", to.BoolFromNumber(0))
fmt.Println("1 ->", to.BoolFromNumber(1))
fmt.Println("-1 ->", to.BoolFromNumber(-1))
fmt.Println("42 ->", to.BoolFromNumber(42))
fmt.Println("0.0 ->", to.BoolFromNumber(0.0))
fmt.Println("0.1 ->", to.BoolFromNumber(0.1))
}
Output: 0 -> false 1 -> true -1 -> true 42 -> true 0.0 -> false 0.1 -> true
func BoolFromString ¶
BoolFromString will convert any string to bool
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting various string representations to bool
trueValue, err := to.BoolFromString("true")
fmt.Println("true ->", trueValue, err)
falseValue, err := to.BoolFromString("false")
fmt.Println("false ->", falseValue, err)
oneValue, err := to.BoolFromString("1")
fmt.Println("1 ->", oneValue, err)
zeroValue, err := to.BoolFromString("0")
fmt.Println("0 ->", zeroValue, err)
invalidValue, err := to.BoolFromString("not-a-bool")
fmt.Println("not-a-bool ->", invalidValue, err != nil)
}
Output: true -> true <nil> false -> false <nil> 1 -> true <nil> 0 -> false <nil> not-a-bool -> false true
func CamelCase ¶
CamelCase converts string to camel case.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Convert to camelCase
val := to.CamelCase("hello world")
fmt.Printf("%T(%q)\n", val, val)
val = to.CamelCase("some-kebab-case")
fmt.Printf("%T(%q)\n", val, val)
val = to.CamelCase("PascalCaseString")
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("helloWorld") string("someKebabCase") string("pascalCaseString")
func Capitalized ¶
Capitalized converts the first character of string to upper case and the remaining to lower case.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Capitalize the first letter of each sentence
val := to.Capitalized("hello")
fmt.Printf("%T(%q)\n", val, val)
val = to.Capitalized("HELLO WORLD")
fmt.Printf("%T(%q)\n", val, val)
val = to.Capitalized("multiple. sentences. here.")
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("Hello") string("Hello world") string("Multiple. Sentences. Here.")
func Ellipsis ¶
Ellipsis trims and truncates a string to a specified length and appends an ellipsis if truncated.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Truncate string with ellipsis
val := to.Ellipsis("This is a short string", 10)
fmt.Printf("%T(%q)\n", val, val)
val = to.Ellipsis("Short", 10)
fmt.Printf("%T(%q)\n", val, val)
val = to.Ellipsis("Little bit longer one, but with small length", 3)
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("This is...") string("Short") string("...")
func EllipsisWith ¶
EllipsisWith returns a function that trims and truncates a string to a specified length and appends an ellipsis if truncated. It's wrapped around Ellipsis function, to make it usable in Map functions. #mapper
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Create a function that truncates strings to 10 characters
truncate := to.EllipsisWith(10)
val := truncate("This is a long string that should be truncated")
fmt.Printf("%T(%q)\n", val, val)
val = truncate("Short")
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("This is...") string("Short")
func EmptyValue ¶
func EmptyValue[T any]() T
EmptyValue returns the zero value of type.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Get zero value of int
val := to.EmptyValue[int]()
fmt.Printf("%T(%d)\n", val, val)
// Get zero value of string
strVal := to.EmptyValue[string]()
fmt.Printf("%T(%q)\n", strVal, strVal)
// Get zero value of bool
boolVal := to.EmptyValue[bool]()
fmt.Printf("%T(%v)\n", boolVal, boolVal)
}
Output: int(0) string("") bool(false)
func Enum ¶ added in v1.7.0
Enum converts the provided value to the available enum value in a case-insensitive manner. In case when the provided value doesn't match any of the enums, it will return an empty string as the enum and error. This is a case-insensitive version if you prefer to be more strict (value matches exactly one of the enums), use to.EnumStrict.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
type FooBarEnum string
const (
Foo FooBarEnum = "foo"
Bar FooBarEnum = "bar"
)
// Case-insensitive match succeeds
v, err := to.Enum("FoO", Foo, Bar)
fmt.Printf("%q, Error: %v\n", v, err)
// No match found -> returns empty string and an error
v, err = to.Enum("baz", Foo, Bar)
fmt.Printf("%q, Error: %v\n", v, err)
}
Output: "foo", Error: <nil> "", Error: invalid value: baz doesn't match enum values [foo bar]
func EnumStrict ¶ added in v1.7.0
EnumStrict converts the provided value to the available enum value in a strict manner (value matches exactly one of the enums). In case when the provided value doesn't match any of the enums, it will return an empty string as the enum and error. This is a strict version if you prefer to be more flexible about value (value matches one of the enums in case-insensitive manner), use to.Enum.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
type FooBarEnum string
const (
Foo FooBarEnum = "foo"
Bar FooBarEnum = "bar"
)
// Case-sensitive match succeeds
v, err := to.EnumStrict("foo", Foo, Bar)
fmt.Printf("%q, Error: %v\n", v, err)
// Case-sensitive mismatch -> returns empty string and an error
v, err = to.EnumStrict("FOO", Foo, Bar)
fmt.Printf("%q, Error: %v\n", v, err)
}
Output: "foo", Error: <nil> "", Error: invalid value: FOO doesn't match enum values [foo bar]
func Float32 ¶ added in v0.7.0
func Float32[V ConvertableToNumber](value V) (float32, error)
Float32 will convert bool, any number or string, and their subtypes to float32
NOTE: This is using reflection, if it is an issue, use dedicated functions for a given type, with suffix like
FromBool or FromSigned.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.Float32(true)
fmt.Printf("Bool: %T(%g), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.Float32("3.14159")
fmt.Printf("String: %T(%g), Error: %v\n", strVal, strVal, strErr)
// Example for float32 value
float32Val, float32Err := to.Float32(float32(3.14159))
fmt.Printf("Float32: %T(%g), Error: %v\n", float32Val, float32Val, float32Err)
// Example for int value
intVal, intErr := to.Float32(3)
fmt.Printf("Int: %T(%g), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.Float32(CustomString("3.14159"))
fmt.Printf("CustomString: %T(%g), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.Float32(CustomInt(3))
fmt.Printf("CustomInt: %T(%g), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.Float32(CustomBool(true))
fmt.Printf("CustomBool: %T(%g), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
}
Output: Bool: float32(1), Error: <nil> String: float32(3.14159), Error: <nil> Float32: float32(3.14159), Error: <nil> Int: float32(3), Error: <nil> CustomString: float32(3.14159), Error: <nil> CustomInt: float32(3), Error: <nil> CustomBool: float32(1), Error: <nil>
func Float32FromBool ¶ added in v1.3.0
Float32FromBool converts a boolean value to its float32 representation (true = 1.0, false = 0.0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.Float32FromBool(true)
fmt.Printf("true: %T(%g)\n", trueVal, trueVal)
// Converting false value
falseVal := to.Float32FromBool(false)
fmt.Printf("false: %T(%g)\n", falseVal, falseVal)
}
Output: true: float32(1) false: float32(0)
func Float32FromSigned ¶ added in v1.2.0
func Float32FromSigned[V types.SignedNumber](value V) (float32, error)
Float32FromSigned will convert any signed number to float32, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Float32FromSigned(3)
fmt.Printf("%T(%g), Error: %v\n", val, val, err)
}
Output: float32(3), Error: <nil>
func Float32FromString ¶
Float32FromString will convert any string to float32, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.Float32FromString("3.14159")
fmt.Printf("%T(%g), Error: %v\n", val, val, err)
}
Output: float32(3.14159), Error: <nil>
func Float32FromUnsigned ¶ added in v0.7.0
Float32FromUnsigned will convert any unassigned number to float
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Float32FromUnsigned(uint(42))
fmt.Printf("%T(%g), Error: %v\n", val, val, err)
}
Output: float32(42), Error: <nil>
func Float64 ¶ added in v0.7.0
func Float64[V ConvertableToNumber](value V) (float64, error)
Float64 will convert bool, any number or string, and their subtypes to float64
NOTE: This is using reflection, if it is an issue, use dedicated functions for a given type, with suffix like
FromBool or FromSigned.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.Float64(true)
fmt.Printf("Bool: %T(%g), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.Float64("3.14159265359")
fmt.Printf("String: %T(%g), Error: %v\n", strVal, strVal, strErr)
// Example for float64 value
float64Val, float64Err := to.Float64(float64(3.14159265359))
fmt.Printf("Float64: %T(%g), Error: %v\n", float64Val, float64Val, float64Err)
// Example for int value
intVal, intErr := to.Float64(3)
fmt.Printf("Int: %T(%g), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.Float64(CustomString("3.14159265359"))
fmt.Printf("CustomString: %T(%g), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.Float64(CustomInt(3))
fmt.Printf("CustomInt: %T(%g), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.Float64(CustomBool(true))
fmt.Printf("CustomBool: %T(%g), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
}
Output: Bool: float64(1), Error: <nil> String: float64(3.14159265359), Error: <nil> Float64: float64(3.14159265359), Error: <nil> Int: float64(3), Error: <nil> CustomString: float64(3.14159265359), Error: <nil> CustomInt: float64(3), Error: <nil> CustomBool: float64(1), Error: <nil>
func Float64FromBool ¶ added in v1.3.0
Float64FromBool converts a boolean value to its float64 representation (true = 1.0, false = 0.0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.Float64FromBool(true)
fmt.Printf("true: %T(%g)\n", trueVal, trueVal)
// Converting false value
falseVal := to.Float64FromBool(false)
fmt.Printf("false: %T(%g)\n", falseVal, falseVal)
}
Output: true: float64(1) false: float64(0)
func Float64FromSigned ¶ added in v1.2.0
func Float64FromSigned[V types.SignedNumber](value V) (float64, error)
Float64FromSigned will convert any signed number to float64
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Float64FromSigned(3)
fmt.Printf("%T(%g), Error: %v\n", val, val, err)
}
Output: float64(3), Error: <nil>
func Float64FromString ¶
Float64FromString will convert any string to float64, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.Float64FromString("3.14159")
fmt.Printf("%T(%g), Error: %v\n", val, val, err)
}
Output: float64(3.14159), Error: <nil>
func Float64FromUnsigned ¶ added in v0.7.0
Float64FromUnsigned will convert any unassigned number to float
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Float64FromUnsigned(uint64(42))
fmt.Printf("%T(%g), Error: %v\n", val, val, err)
}
Output: float64(42), Error: <nil>
func Int ¶
func Int[V ConvertableToNumber](value V) (int, error)
Int will convert bool, any number or string, and their subtypes to int
NOTE: This is using reflection, if it is an issue, use dedicated functions for a given type, with suffix like
FromBool or FromSigned.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.Int(true)
fmt.Printf("Bool: %T(%d), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.Int("42")
fmt.Printf("String: %T(%d), Error: %v\n", strVal, strVal, strErr)
// Example for uint64 value
uint64Val, uint64Err := to.Int(uint64(9223372036854775807))
fmt.Printf("Uint64: %T(%d), Error: %v\n", uint64Val, uint64Val, uint64Err)
// Example for int64 value
int64Val, int64Err := to.Int(int64(9223372036854775807))
fmt.Printf("Int64: %T(%d), Error: %v\n", int64Val, int64Val, int64Err)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.Int(CustomString("42"))
fmt.Printf("CustomString: %T(%d), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int64
type CustomInt64 int64
customInt64Val, customInt64Err := to.Int(CustomInt64(42))
fmt.Printf("CustomInt64: %T(%d), Error: %v\n", customInt64Val, customInt64Val, customInt64Err)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.Int(CustomBool(true))
fmt.Printf("CustomBool: %T(%d), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
}
Output: Bool: int(1), Error: <nil> String: int(42), Error: <nil> Uint64: int(9223372036854775807), Error: <nil> Int64: int(9223372036854775807), Error: <nil> CustomString: int(42), Error: <nil> CustomInt64: int(42), Error: <nil> CustomBool: int(1), Error: <nil>
func Int16 ¶
func Int16[V ConvertableToNumber](value V) (int16, error)
Int16 will convert bool, any number or string, and their subtypes to int16
NOTE: This is using reflection, if it is an issue, use dedicated functions for a given type, with suffix like
FromBool or FromSigned.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.Int16(true)
fmt.Printf("Bool: %T(%d), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.Int16("1000")
fmt.Printf("String: %T(%d), Error: %v\n", strVal, strVal, strErr)
// Example for uint16 value
uint16Val, uint16Err := to.Int16(uint16(1000))
fmt.Printf("Uint16: %T(%d), Error: %v\n", uint16Val, uint16Val, uint16Err)
// Example for int value
intVal, intErr := to.Int16(1000)
fmt.Printf("Int: %T(%d), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.Int16(CustomString("1000"))
fmt.Printf("CustomString: %T(%d), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.Int16(CustomInt(1000))
fmt.Printf("CustomInt: %T(%d), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.Int16(CustomBool(true))
fmt.Printf("CustomBool: %T(%d), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
// Converting out of range
valOOR, errOOR := to.Int16(40000)
fmt.Printf("Out of range: %T(%d), Error: %v\n", valOOR, valOOR, errors.Is(errOOR, to.ErrValueOutOfRange))
}
Output: Bool: int16(1), Error: <nil> String: int16(1000), Error: <nil> Uint16: int16(1000), Error: <nil> Int: int16(1000), Error: <nil> CustomString: int16(1000), Error: <nil> CustomInt: int16(1000), Error: <nil> CustomBool: int16(1), Error: <nil> Out of range: int16(0), Error: true
func Int16FromBool ¶ added in v1.3.0
Int16FromBool converts a boolean value to its int16 representation (true = 1, false = 0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.Int16FromBool(true)
fmt.Printf("true: %T(%d)\n", trueVal, trueVal)
// Converting false value
falseVal := to.Int16FromBool(false)
fmt.Printf("false: %T(%d)\n", falseVal, falseVal)
}
Output: true: int16(1) false: int16(0)
func Int16FromSigned ¶ added in v1.2.0
func Int16FromSigned[V types.SignedNumber](value V) (int16, error)
Int16FromSigned will convert any signed number to int16, with range checks
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Int16FromSigned(1000)
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
// Converting out of range
valOOR, errOOR := to.Int16FromSigned(40000)
fmt.Printf("%T(%d), Error: %v\n", valOOR, valOOR, errors.Is(errOOR, to.ErrValueOutOfRange))
}
Output: int16(1000), Error: <nil> int16(0), Error: true
func Int16FromString ¶
Int16FromString will convert any string to int16, with range checks
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.Int16FromString("12345")
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
// Invalid syntax
valSyntax, errSyntax := to.Int16FromString("abc")
fmt.Printf("%T(%d), Error: %v\n", valSyntax, valSyntax, errors.Is(errSyntax, to.ErrInvalidStringSyntax))
}
Output: int16(12345), Error: <nil> int16(0), Error: true
func Int16FromUnsigned ¶
Int16FromUnsigned will convert any unsigned integer to int16, with range checks.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Int16FromUnsigned(uint(1000))
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
// Converting out of range
valOOR, errOOR := to.Int16FromUnsigned(uint(40000))
fmt.Printf("%T(%d), Error: %v\n", valOOR, valOOR, errors.Is(errOOR, to.ErrValueOutOfRange))
}
Output: int16(1000), Error: <nil> int16(0), Error: true
func Int32 ¶
func Int32[V ConvertableToNumber](value V) (int32, error)
Int32 will convert bool, any number or string, and their subtypes to int32
NOTE: This is using reflection, if it is an issue, use dedicated functions for a given type, with suffix like
FromBool or FromSigned.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.Int32(true)
fmt.Printf("Bool: %T(%d), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.Int32("1000000")
fmt.Printf("String: %T(%d), Error: %v\n", strVal, strVal, strErr)
// Example for uint32 value
uint32Val, uint32Err := to.Int32(uint32(1000000))
fmt.Printf("Uint32: %T(%d), Error: %v\n", uint32Val, uint32Val, uint32Err)
// Example for int value
intVal, intErr := to.Int32(1000000)
fmt.Printf("Int: %T(%d), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.Int32(CustomString("1000000"))
fmt.Printf("CustomString: %T(%d), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.Int32(CustomInt(1000000))
fmt.Printf("CustomInt: %T(%d), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.Int32(CustomBool(true))
fmt.Printf("CustomBool: %T(%d), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
}
Output: Bool: int32(1), Error: <nil> String: int32(1000000), Error: <nil> Uint32: int32(1000000), Error: <nil> Int: int32(1000000), Error: <nil> CustomString: int32(1000000), Error: <nil> CustomInt: int32(1000000), Error: <nil> CustomBool: int32(1), Error: <nil>
func Int32FromBool ¶ added in v1.3.0
Int32FromBool converts a boolean value to its int32 representation (true = 1, false = 0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.Int32FromBool(true)
fmt.Printf("true: %T(%d)\n", trueVal, trueVal)
// Converting false value
falseVal := to.Int32FromBool(false)
fmt.Printf("false: %T(%d)\n", falseVal, falseVal)
}
Output: true: int32(1) false: int32(0)
func Int32FromSigned ¶ added in v1.2.0
func Int32FromSigned[V types.SignedNumber](value V) (int32, error)
Int32FromSigned will convert any signed number to int32, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Int32FromSigned(1000000)
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: int32(1000000), Error: <nil>
func Int32FromString ¶
Int32FromString will convert any string to int32, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.Int32FromString("1234567")
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: int32(1234567), Error: <nil>
func Int32FromUnsigned ¶
Int32FromUnsigned will convert any unsigned integer to int32, with range checks.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Int32FromUnsigned(uint(1000000))
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: int32(1000000), Error: <nil>
func Int64 ¶
func Int64[V ConvertableToNumber](value V) (int64, error)
Int64 will convert bool, any number or string, and their subtypes to int64
NOTE: This is using reflection,
if it is an issue, use dedicated functions for a given type, with suffix like FromBool or FromSigned.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.Int64(true)
fmt.Printf("Bool: %T(%d), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.Int64("9223372036854775807")
fmt.Printf("String: %T(%d), Error: %v\n", strVal, strVal, strErr)
// Example for uint64 value
uint64Val, uint64Err := to.Int64(uint64(9223372036854775807))
fmt.Printf("Uint64: %T(%d), Error: %v\n", uint64Val, uint64Val, uint64Err)
// Example for int value
intVal, intErr := to.Int64(9223372036854775807)
fmt.Printf("Int: %T(%d), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.Int64(CustomString("9223372036854775807"))
fmt.Printf("CustomString: %T(%d), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.Int64(CustomInt(9223372036854775807))
fmt.Printf("CustomInt: %T(%d), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.Int64(CustomBool(true))
fmt.Printf("CustomBool: %T(%d), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
}
Output: Bool: int64(1), Error: <nil> String: int64(9223372036854775807), Error: <nil> Uint64: int64(9223372036854775807), Error: <nil> Int: int64(9223372036854775807), Error: <nil> CustomString: int64(9223372036854775807), Error: <nil> CustomInt: int64(9223372036854775807), Error: <nil> CustomBool: int64(1), Error: <nil>
func Int64FromBool ¶ added in v1.3.0
Int64FromBool converts a boolean value to its int64 representation (true = 1, false = 0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.Int64FromBool(true)
fmt.Printf("true: %T(%d)\n", trueVal, trueVal)
// Converting false value
falseVal := to.Int64FromBool(false)
fmt.Printf("false: %T(%d)\n", falseVal, falseVal)
}
Output: true: int64(1) false: int64(0)
func Int64FromSigned ¶ added in v1.2.0
func Int64FromSigned[V types.SignedNumber](value V) (int64, error)
Int64FromSigned will convert any signed number to int64, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Int64FromSigned(9223372036854775807)
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: int64(9223372036854775807), Error: <nil>
func Int64FromString ¶
Int64FromString will convert any string to int64, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.Int64FromString("9223372036854775807")
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: int64(9223372036854775807), Error: <nil>
func Int64FromUnsigned ¶
Int64FromUnsigned will convert any unsigned integer to int64, with range checks.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Int64FromUnsigned(uint64(9223372036854775807))
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: int64(9223372036854775807), Error: <nil>
func Int8 ¶
func Int8[V ConvertableToNumber](value V) (int8, error)
Int8 will convert bool, any number or string, and their subtypes to int8
NOTE: This is using reflection,
if it is an issue, use dedicated functions for a given type, with suffix like FromBool or FromSigned.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.Int8(true)
fmt.Printf("Bool: %T(%d), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.Int8("42")
fmt.Printf("String: %T(%d), Error: %v\n", strVal, strVal, strErr)
// Example for uint8 value
uint8Val, uint8Err := to.Int8(uint8(42))
fmt.Printf("Uint8: %T(%d), Error: %v\n", uint8Val, uint8Val, uint8Err)
// Example for int value
intVal, intErr := to.Int8(42)
fmt.Printf("Int: %T(%d), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.Int8(CustomString("42"))
fmt.Printf("CustomString: %T(%d), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.Int8(CustomInt(42))
fmt.Printf("CustomInt: %T(%d), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.Int8(CustomBool(true))
fmt.Printf("CustomBool: %T(%d), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
// Converting out of range
valOOR, errOOR := to.Int8(1000)
fmt.Printf("Out of range: %T(%d), Error: %v\n", valOOR, valOOR, errors.Is(errOOR, to.ErrValueOutOfRange))
}
Output: Bool: int8(1), Error: <nil> String: int8(42), Error: <nil> Uint8: int8(42), Error: <nil> Int: int8(42), Error: <nil> CustomString: int8(42), Error: <nil> CustomInt: int8(42), Error: <nil> CustomBool: int8(1), Error: <nil> Out of range: int8(0), Error: true
func Int8FromBool ¶ added in v1.3.0
Int8FromBool converts a boolean value to its int8 representation (true = 1, false = 0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.Int8FromBool(true)
fmt.Printf("true: %T(%d)\n", trueVal, trueVal)
// Converting false value
falseVal := to.Int8FromBool(false)
fmt.Printf("false: %T(%d)\n", falseVal, falseVal)
}
Output: true: int8(1) false: int8(0)
func Int8FromSigned ¶ added in v1.2.0
func Int8FromSigned[V types.SignedNumber](value V) (int8, error)
Int8FromSigned will convert any signed number to int8, with range checks
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Int8FromSigned(42)
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
// Converting out of range
valOOR, errOOR := to.Int8FromSigned(1000)
fmt.Printf("%T(%d), Error: %v\n", valOOR, valOOR, errors.Is(errOOR, to.ErrValueOutOfRange))
}
Output: int8(42), Error: <nil> int8(0), Error: true
func Int8FromString ¶
Int8FromString will convert any string to int8, with range checks
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.Int8FromString("100")
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
// Out of range
valOOR, errOOR := to.Int8FromString("200")
fmt.Printf("%T(%d), Error: %v\n", valOOR, valOOR, errors.Is(errOOR, to.ErrValueOutOfRange))
}
Output: int8(100), Error: <nil> int8(0), Error: true
func Int8FromUnsigned ¶
Int8FromUnsigned will convert any unsigned integer to int8, with range checks.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.Int8FromUnsigned(uint(42))
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
// Converting out of range
valOOR, errOOR := to.Int8FromUnsigned(uint(200))
fmt.Printf("%T(%d), Error: %v\n", valOOR, valOOR, errors.Is(errOOR, to.ErrValueOutOfRange))
}
Output: int8(42), Error: <nil> int8(0), Error: true
func IntFromBool ¶ added in v1.2.0
IntFromBool converts a boolean value to its integer representation (true = 1, false = 0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.IntFromBool(true)
fmt.Printf("true: %T(%d)\n", trueVal, trueVal)
// Converting false value
falseVal := to.IntFromBool(false)
fmt.Printf("false: %T(%d)\n", falseVal, falseVal)
}
Output: true: int(1) false: int(0)
func IntFromSigned ¶ added in v1.2.0
func IntFromSigned[V types.SignedNumber](value V) (int, error)
IntFromSigned will convert any signed number to int, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.IntFromSigned(int16(42))
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: int(42), Error: <nil>
func IntFromString ¶
IntFromString will convert any string to int, with range checks
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.IntFromString("12345")
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
// Invalid syntax
valSyntax, errSyntax := to.IntFromString("abc")
fmt.Printf("%T(%d), Error: %v\n", valSyntax, valSyntax, errors.Is(errSyntax, to.ErrInvalidStringSyntax))
}
Output: int(12345), Error: <nil> int(0), Error: true
func IntFromUnsigned ¶
IntFromUnsigned will convert any unsigned integer to int, with range checks.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.IntFromUnsigned(uint16(42))
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: int(42), Error: <nil>
func KebabCase ¶
KebabCase converts string to kebab case.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Convert to kebab-case
val := to.KebabCase("hello world")
fmt.Printf("%T(%q)\n", val, val)
val = to.KebabCase("camelCaseString")
fmt.Printf("%T(%q)\n", val, val)
val = to.KebabCase("PascalCaseString")
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("hello-world") string("camel-case-string") string("pascal-case-string")
func Nil ¶
func Nil[T any]() *T
Nil returns a nil pointer of type.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Create a nil pointer of int type
val := to.Nil[int]()
fmt.Printf("%T, IsNil: %v\n", val, val == nil)
// Create a nil pointer of a struct type
type Person struct {
Name string
Age int
}
person := to.Nil[Person]()
fmt.Printf("%T, IsNil: %v\n", person, person == nil)
}
Output: *int, IsNil: true *to_test.Person, IsNil: true
func NilOfType ¶
func NilOfType[T any](_ *T) *T
NilOfType returns a nil pointer of type.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Create a nil pointer based on existing pointer type
var existingPtr *string
nilPtr := to.NilOfType(existingPtr)
fmt.Printf("%T, IsNil: %v\n", nilPtr, nilPtr == nil)
}
Output: *string, IsNil: true
func NoLessThan ¶
NoLessThan will return the value if it's not less than the min value or the min value.
func NoMoreThan ¶
NoMoreThan will return the value if it's not more than the max value or the max value.
func Options ¶ added in v0.6.0
func Options[T any](opts ...func(*T)) T
Options helper function to handle options pattern.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Prepare definition of config
type Config struct {
Host string
Port int
Debug bool
}
// Define reusable option functions
withHost := func(host string) func(*Config) {
return func(c *Config) {
c.Host = host
}
}
withPort := func(port int) func(*Config) {
return func(c *Config) {
c.Port = port
}
}
withDebug := func(c *Config) {
c.Debug = true
}
// Handle opts
config := to.Options[Config](
withHost("example.com"),
withPort(443),
withDebug,
)
fmt.Printf("Host: %s\n", config.Host)
fmt.Printf("Port: %d\n", config.Port)
fmt.Printf("Debug: %v\n", config.Debug)
}
Output: Host: example.com Port: 443 Debug: true
func OptionsWithDefault ¶ added in v0.6.0
func OptionsWithDefault[T any](defaultOptions T, opts ...func(*T)) T
OptionsWithDefault helper function to handle options pattern with default value for options.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
type Config struct {
Host string
Port int
Debug bool
}
// Default configuration
defaultConfig := Config{
Host: "127.0.0.1",
Port: 3000,
Debug: false,
}
// Override some default values using OptionsWithDefault
config := to.OptionsWithDefault(defaultConfig,
func(c *Config) {
c.Port = 8080
},
func(c *Config) {
c.Debug = true
},
)
fmt.Printf("Host: %s\n", config.Host)
fmt.Printf("Port: %d\n", config.Port)
fmt.Printf("Debug: %v\n", config.Debug)
}
Output: Host: 127.0.0.1 Port: 8080 Debug: true
func PascalCase ¶
PascalCase converts string to pascal case.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Convert to PascalCase
val := to.PascalCase("hello world")
fmt.Printf("%T(%q)\n", val, val)
val = to.PascalCase("some-kebab-case")
fmt.Printf("%T(%q)\n", val, val)
val = to.PascalCase("snake_case_string")
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("HelloWorld") string("SomeKebabCase") string("SnakeCaseString")
func Ptr ¶
func Ptr[T any](x T) *T
Ptr returns a pointer copy of value.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Create a pointer to a value
val := to.Ptr(42)
fmt.Printf("%T(%d)\n", val, *val)
// Works with strings too
strVal := to.Ptr("hello")
fmt.Printf("%T(%q)\n", strVal, *strVal)
}
Output: *int(42) *string("hello")
func Sentences ¶
Sentences splits string into an array of its sentences. The sentences are trimmed from leading and trailing spaces.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Splitting into sentences
text := "Hello, world! This is a test. Is this working?"
sentences := to.Sentences(text)
fmt.Printf("%#v\n", sentences)
}
Output: []string{"Hello, world!", "This is a test.", "Is this working?"}
func SliceOfAny ¶
SliceOfAny casts the slice to a slice of any type.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Convert a slice of ints to a slice of any
values := []int{1, 2, 3}
anySlice := to.SliceOfAny(values)
fmt.Printf("Type: %T, Length: %d\n", anySlice, len(anySlice))
fmt.Printf("First element type: %T\n", anySlice[0])
}
Output: Type: []interface {}, Length: 3 First element type: int
func SliceOfPtr ¶
func SliceOfPtr[T any](collection []T) []*T
SliceOfPtr returns a slice of pointer copy of value.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Convert a slice of values to a slice of pointers
values := []int{1, 2, 3}
ptrs := to.SliceOfPtr(values)
// Print first element to demonstrate it's a pointer
fmt.Printf("%T(%d)\n", ptrs[0], *ptrs[0])
// Print length to show all elements were converted
fmt.Printf("Length: %d\n", len(ptrs))
}
Output: *int(1) Length: 3
func SliceOfValue ¶
func SliceOfValue[T any](collection []*T) []T
SliceOfValue returns a slice with the pointer values.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Convert a slice of pointers to a slice of values
p1, p2 := to.Ptr(10), to.Ptr(20)
ptrs := []*int{p1, p2, nil}
values := to.SliceOfValue(ptrs)
fmt.Printf("%T(%d)\n", values[0], values[0])
fmt.Printf("%T(%d)\n", values[1], values[1])
fmt.Printf("%T(%d) (zero value from nil)\n", values[2], values[2])
}
Output: int(10) int(20) int(0) (zero value from nil)
func SnakeCase ¶
SnakeCase converts string to snake case.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Convert to snake_case
val := to.SnakeCase("hello world")
fmt.Printf("%T(%q)\n", val, val)
val = to.SnakeCase("camelCaseString")
fmt.Printf("%T(%q)\n", val, val)
val = to.SnakeCase("PascalCaseString")
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("hello_world") string("camel_case_string") string("pascal_case_string")
func String ¶ added in v1.7.0
String converts any value to string. It takes into account some interfaces like: encoding.TextMarshaler
if it can produce text without returning an error it will be the preferred result. if it returns the error, it will fallback to other methods to convert to string. The support for encoding.TextMarshaler is experimental and may change in the future
fmt.Stringer - is preferred over default string conversion
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
type exampleStringer string
func (s exampleStringer) String() string {
return "stringer-" + string(s)
}
type exampleTextMarshaler string
func (m exampleTextMarshaler) MarshalText() ([]byte, error) {
if string(m) != "hello" {
return nil, fmt.Errorf("this example allows only hello value")
}
return []byte("text-marshaler-" + string(m)), nil
}
func (m exampleTextMarshaler) String() string {
return "fallback-on-fail-marshal-text"
}
func main() {
var str string
// String type
str = to.String("hello")
fmt.Printf("%q\n", str)
// Custom string type
type CustomString string
str = to.String(CustomString("hello-custom"))
fmt.Printf("%q\n", str)
// fmt.Stringer
str = to.String(exampleStringer("hello"))
fmt.Printf("%q\n", str)
// TextMarshaler
str = to.String(exampleTextMarshaler("hello"))
fmt.Printf("%q\n", str)
// TextMarshaler with error when marshaling will fallback to other methods of stringifying
// This is experimental behavior and may change in the future
str = to.String(exampleTextMarshaler("failing-marshal-text"))
fmt.Printf("%q\n", str)
// Integer type
str = to.String(42)
fmt.Printf("%q\n", str)
// Unsigned integer type
str = to.String(uint(123))
fmt.Printf("%q\n", str)
// Float type
str = to.String(3.14159)
fmt.Printf("%q\n", str)
// Boolean type
str = to.String(true)
fmt.Printf("%q\n", str)
}
Output: "hello" "hello-custom" "stringer-hello" "text-marshaler-hello" "fallback-on-fail-marshal-text" "42" "123" "3.14159" "true"
func StringFromBool ¶
StringFromBool will convert any bool to string
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Boolean to string conversion
val := to.StringFromBool(true)
fmt.Printf("%T(%q)\n", val, val)
val = to.StringFromBool(false)
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("true") string("false")
func StringFromBytes ¶
StringFromBytes will convert any byte slice to string
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Byte slice to string conversion
val := to.StringFromBytes([]byte("hello"))
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("hello")
func StringFromFloat ¶
StringFromFloat will convert any float to string
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Float to string conversion
val := to.StringFromFloat(3.14159)
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("3.141590")
func StringFromInteger ¶
StringFromInteger will convert any integer to string
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Integer to string conversion
val := to.StringFromInteger(42)
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("42")
func StringFromRune ¶
StringFromRune will convert any rune to string
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Rune to string conversion
val := to.StringFromRune('世')
fmt.Printf("%T(%q)\n", val, val)
}
Output: string("世")
func UInt ¶
func UInt[V ConvertableToNumber](value V) (uint, error)
UInt will convert bool, any number or string, and their subtypes to uint
NOTE: This is using reflection, if it is an issue, use dedicated functions for a given type, with suffix like
FromBool or FromNumber.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.UInt(true)
fmt.Printf("Bool: %T(%d), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.UInt("42")
fmt.Printf("String: %T(%d), Error: %v\n", strVal, strVal, strErr)
// Example for uint value
uintVal, uintErr := to.UInt(uint(42))
fmt.Printf("Uint: %T(%d), Error: %v\n", uintVal, uintVal, uintErr)
// Example for int value
intVal, intErr := to.UInt(42)
fmt.Printf("Int: %T(%d), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.UInt(CustomString("42"))
fmt.Printf("CustomString: %T(%d), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.UInt(CustomInt(42))
fmt.Printf("CustomInt: %T(%d), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.UInt(CustomBool(true))
fmt.Printf("CustomBool: %T(%d), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
// Converting negative value
valNeg, errNeg := to.UInt(-5)
fmt.Printf("Negative: %T(%d), Error: %v\n", valNeg, valNeg, errors.Is(errNeg, to.ErrValueOutOfRange))
}
Output: Bool: uint(1), Error: <nil> String: uint(42), Error: <nil> Uint: uint(42), Error: <nil> Int: uint(42), Error: <nil> CustomString: uint(42), Error: <nil> CustomInt: uint(42), Error: <nil> CustomBool: uint(1), Error: <nil> Negative: uint(0), Error: true
func UInt16 ¶
func UInt16[V ConvertableToNumber](value V) (uint16, error)
UInt16 will convert bool, any number or string, and their subtypes to uint16
NOTE: This is using reflection, if it is an issue, use dedicated functions for a given type, with suffix like
FromBool or FromNumber.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.UInt16(true)
fmt.Printf("Bool: %T(%d), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.UInt16("65000")
fmt.Printf("String: %T(%d), Error: %v\n", strVal, strVal, strErr)
// Example for uint16 value
uint16Val, uint16Err := to.UInt16(uint16(65000))
fmt.Printf("Uint16: %T(%d), Error: %v\n", uint16Val, uint16Val, uint16Err)
// Example for int value
intVal, intErr := to.UInt16(65000)
fmt.Printf("Int: %T(%d), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.UInt16(CustomString("65000"))
fmt.Printf("CustomString: %T(%d), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.UInt16(CustomInt(65000))
fmt.Printf("CustomInt: %T(%d), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.UInt16(CustomBool(true))
fmt.Printf("CustomBool: %T(%d), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
}
Output: Bool: uint16(1), Error: <nil> String: uint16(65000), Error: <nil> Uint16: uint16(65000), Error: <nil> Int: uint16(65000), Error: <nil> CustomString: uint16(65000), Error: <nil> CustomInt: uint16(65000), Error: <nil> CustomBool: uint16(1), Error: <nil>
func UInt16FromBool ¶ added in v1.3.0
UInt16FromBool converts a boolean value to its uint16 representation (true = 1, false = 0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.UInt16FromBool(true)
fmt.Printf("true: %T(%d)\n", trueVal, trueVal)
// Converting false value
falseVal := to.UInt16FromBool(false)
fmt.Printf("false: %T(%d)\n", falseVal, falseVal)
}
Output: true: uint16(1) false: uint16(0)
func UInt16FromNumber ¶ added in v1.2.0
UInt16FromNumber will convert any number to uint16, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.UInt16FromNumber(65000)
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: uint16(65000), Error: <nil>
func UInt16FromString ¶
UInt16FromString will convert any string to uint16, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.UInt16FromString("65000")
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: uint16(65000), Error: <nil>
func UInt32 ¶
func UInt32[V ConvertableToNumber](value V) (uint32, error)
UInt32 will convert bool, any number or string, and their subtypes to uint32
NOTE: This is using reflection, if it is an issue, use dedicated functions for a given type, with suffix like
FromBool or FromNumber.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.UInt32(true)
fmt.Printf("Bool: %T(%d), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.UInt32("4294967295")
fmt.Printf("String: %T(%d), Error: %v\n", strVal, strVal, strErr)
// Example for uint32 value
uint32Val, uint32Err := to.UInt32(uint32(4294967295))
fmt.Printf("Uint32: %T(%d), Error: %v\n", uint32Val, uint32Val, uint32Err)
// Example for int value
intVal, intErr := to.UInt32(42)
fmt.Printf("Int: %T(%d), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.UInt32(CustomString("42"))
fmt.Printf("CustomString: %T(%d), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.UInt32(CustomInt(42))
fmt.Printf("CustomInt: %T(%d), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.UInt32(CustomBool(true))
fmt.Printf("CustomBool: %T(%d), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
// Negative number
valNeg, errNeg := to.UInt32(-5)
fmt.Printf("Negative: %T(%d), Error: %v\n", valNeg, valNeg, errors.Is(errNeg, to.ErrValueOutOfRange))
}
Output: Bool: uint32(1), Error: <nil> String: uint32(4294967295), Error: <nil> Uint32: uint32(4294967295), Error: <nil> Int: uint32(42), Error: <nil> CustomString: uint32(42), Error: <nil> CustomInt: uint32(42), Error: <nil> CustomBool: uint32(1), Error: <nil> Negative: uint32(0), Error: true
func UInt32FromBool ¶ added in v1.3.0
UInt32FromBool converts a boolean value to its uint32 representation (true = 1, false = 0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.UInt32FromBool(true)
fmt.Printf("true: %T(%d)\n", trueVal, trueVal)
// Converting false value
falseVal := to.UInt32FromBool(false)
fmt.Printf("false: %T(%d)\n", falseVal, falseVal)
}
Output: true: uint32(1) false: uint32(0)
func UInt32FromNumber ¶ added in v1.2.0
UInt32FromNumber will convert any number to uint32, with range checks
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.UInt32FromNumber(42)
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
// Negative number
valNeg, errNeg := to.UInt32FromNumber(-5)
fmt.Printf("%T(%d), Error: %v\n", valNeg, valNeg, errors.Is(errNeg, to.ErrValueOutOfRange))
}
Output: uint32(42), Error: <nil> uint32(0), Error: true
func UInt32FromString ¶
UInt32FromString will convert any string to uint32, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.UInt32FromString("4294967295")
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: uint32(4294967295), Error: <nil>
func UInt64 ¶
func UInt64[V ConvertableToNumber](value V) (uint64, error)
UInt64 will convert bool, any number or string, and their subtypes to uint64
NOTE: This is using reflection, if it is an issue, use dedicated functions for a given type, with suffix like
FromBool or FromNumber.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.UInt64(true)
fmt.Printf("Bool: %T(%d), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.UInt64("18446744073709551615")
fmt.Printf("String: %T(%d), Error: %v\n", strVal, strVal, strErr)
// Example for uint64 value
uint64Val, uint64Err := to.UInt64(uint64(18446744073709551615))
fmt.Printf("Uint64: %T(%d), Error: %v\n", uint64Val, uint64Val, uint64Err)
// Example for int value
intVal, intErr := to.UInt64(42)
fmt.Printf("Int: %T(%d), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.UInt64(CustomString("42"))
fmt.Printf("CustomString: %T(%d), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.UInt64(CustomInt(42))
fmt.Printf("CustomInt: %T(%d), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.UInt64(CustomBool(true))
fmt.Printf("CustomBool: %T(%d), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
}
Output: Bool: uint64(1), Error: <nil> String: uint64(18446744073709551615), Error: <nil> Uint64: uint64(18446744073709551615), Error: <nil> Int: uint64(42), Error: <nil> CustomString: uint64(42), Error: <nil> CustomInt: uint64(42), Error: <nil> CustomBool: uint64(1), Error: <nil>
func UInt64FromBool ¶ added in v1.3.0
UInt64FromBool converts a boolean value to its uint64 representation (true = 1, false = 0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.UInt64FromBool(true)
fmt.Printf("true: %T(%d)\n", trueVal, trueVal)
// Converting false value
falseVal := to.UInt64FromBool(false)
fmt.Printf("false: %T(%d)\n", falseVal, falseVal)
}
Output: true: uint64(1) false: uint64(0)
func UInt64FromNumber ¶ added in v1.2.0
UInt64FromNumber will convert any number to uint64, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.UInt64FromNumber(uint(18446744073709551000))
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: uint64(18446744073709551000), Error: <nil>
func UInt64FromString ¶
UInt64FromString will convert any string to uint64, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.UInt64FromString("18446744073709551615")
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: uint64(18446744073709551615), Error: <nil>
func UInt8 ¶
func UInt8[V ConvertableToNumber](value V) (uint8, error)
UInt8 will convert bool, any number or string, and their subtypes to uint8
NOTE: This is using reflection, if it is an issue, use dedicated functions for a given type, with suffix like
FromBool or FromNumber.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Example for bool value
boolVal, boolErr := to.UInt8(true)
fmt.Printf("Bool: %T(%d), Error: %v\n", boolVal, boolVal, boolErr)
// Example for string value
strVal, strErr := to.UInt8("200")
fmt.Printf("String: %T(%d), Error: %v\n", strVal, strVal, strErr)
// Example for uint8 value
uint8Val, uint8Err := to.UInt8(uint8(200))
fmt.Printf("Uint8: %T(%d), Error: %v\n", uint8Val, uint8Val, uint8Err)
// Example for int value
intVal, intErr := to.UInt8(200)
fmt.Printf("Int: %T(%d), Error: %v\n", intVal, intVal, intErr)
// Example for custom type based on string
type CustomString string
customStrVal, customStrErr := to.UInt8(CustomString("200"))
fmt.Printf("CustomString: %T(%d), Error: %v\n", customStrVal, customStrVal, customStrErr)
// Example for custom type based on int
type CustomInt int
customIntVal, customIntErr := to.UInt8(CustomInt(200))
fmt.Printf("CustomInt: %T(%d), Error: %v\n", customIntVal, customIntVal, customIntErr)
// Example for custom type based on bool
type CustomBool bool
customBoolVal, customBoolErr := to.UInt8(CustomBool(true))
fmt.Printf("CustomBool: %T(%d), Error: %v\n", customBoolVal, customBoolVal, customBoolErr)
// Converting out of range
valOOR, errOOR := to.UInt8(300)
fmt.Printf("Out of range: %T(%d), Error: %v\n", valOOR, valOOR, errors.Is(errOOR, to.ErrValueOutOfRange))
}
Output: Bool: uint8(1), Error: <nil> String: uint8(200), Error: <nil> Uint8: uint8(200), Error: <nil> Int: uint8(200), Error: <nil> CustomString: uint8(200), Error: <nil> CustomInt: uint8(200), Error: <nil> CustomBool: uint8(1), Error: <nil> Out of range: uint8(0), Error: true
func UInt8FromBool ¶ added in v1.3.0
UInt8FromBool converts a boolean value to its uint8 representation (true = 1, false = 0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.UInt8FromBool(true)
fmt.Printf("true: %T(%d)\n", trueVal, trueVal)
// Converting false value
falseVal := to.UInt8FromBool(false)
fmt.Printf("false: %T(%d)\n", falseVal, falseVal)
}
Output: true: uint8(1) false: uint8(0)
func UInt8FromNumber ¶ added in v1.2.0
UInt8FromNumber will convert any number to uint8, with range checks
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting within range
val, err := to.UInt8FromNumber(200)
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
// Converting out of range
valOOR, errOOR := to.UInt8FromNumber(300)
fmt.Printf("%T(%d), Error: %v\n", valOOR, valOOR, errors.Is(errOOR, to.ErrValueOutOfRange))
}
Output: uint8(200), Error: <nil> uint8(0), Error: true
func UInt8FromString ¶
UInt8FromString will convert any string to uint8, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.UInt8FromString("200")
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: uint8(200), Error: <nil>
func UIntFromBool ¶ added in v1.3.0
UIntFromBool converts a boolean value to its uint representation (true = 1, false = 0).
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting true value
trueVal := to.UIntFromBool(true)
fmt.Printf("true: %T(%d)\n", trueVal, trueVal)
// Converting false value
falseVal := to.UIntFromBool(false)
fmt.Printf("false: %T(%d)\n", falseVal, falseVal)
}
Output: true: uint(1) false: uint(0)
func UIntFromNumber ¶ added in v1.2.0
UIntFromNumber will convert any number to uint, with range checks
Example ¶
package main
import (
"errors"
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Converting positive value
val, err := to.UIntFromNumber(42)
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
// Converting negative value
valNeg, errNeg := to.UIntFromNumber(-5)
fmt.Printf("%T(%d), Error: %v\n", valNeg, valNeg, errors.Is(errNeg, to.ErrValueOutOfRange))
}
Output: uint(42), Error: <nil> uint(0), Error: true
func UIntFromString ¶
UIntFromString will convert any string to uint, with range checks
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Valid conversion
val, err := to.UIntFromString("42")
fmt.Printf("%T(%d), Error: %v\n", val, val, err)
}
Output: uint(42), Error: <nil>
func Value ¶
func Value[T any](x *T) T
Value returns the pointer value or zero value.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Get value from a pointer
ptr := to.Ptr(42)
val := to.Value(ptr)
fmt.Printf("%T(%d)\n", val, val)
// Get value from a nil pointer (returns zero value)
var nilPtr *string
strVal := to.Value(nilPtr)
fmt.Printf("%T(%q)\n", strVal, strVal)
}
Output: int(42) string("")
func ValueAtLeast ¶ added in v1.2.0
ValueAtLeast will return the value if it's not less than the min value or the min value.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Already above min
val := to.ValueAtLeast(5, 0)
fmt.Printf("ValueAtLeast(5, 0) = %d\n", val)
// Below min
val = to.ValueAtLeast(-5, 0)
fmt.Printf("ValueAtLeast(-5, 0) = %d\n", val)
// Works with float values too
floatVal := to.ValueAtLeast(3.14, 4.0)
fmt.Printf("ValueAtLeast(3.14, 4.0) = %.2f\n", floatVal)
}
Output: ValueAtLeast(5, 0) = 5 ValueAtLeast(-5, 0) = 0 ValueAtLeast(3.14, 4.0) = 4.00
func ValueAtMost ¶ added in v1.2.0
ValueAtMost will return the value if it's not more than the max value or the max value.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Below max
val := to.ValueAtMost(5, 10)
fmt.Printf("ValueAtMost(5, 10) = %d\n", val)
// Above max
val = to.ValueAtMost(15, 10)
fmt.Printf("ValueAtMost(15, 10) = %d\n", val)
}
Output: ValueAtMost(5, 10) = 5 ValueAtMost(15, 10) = 10
func ValueBetween ¶ added in v0.14.0
ValueBetween will clamp the value between the min and max values. In other words it ensures that result is min <= value <= max. For value that is less than min, it will return min. For value that is greater than max, it will return max.
See Also: ValueBetweenThe to use it in Map functions.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Within range
val := to.ValueBetween(5, 0, 10)
fmt.Printf("ValueBetween(5, 0, 10) = %d\n", val)
// Lower than min
val = to.ValueBetween(-5, 0, 10)
fmt.Printf("ValueBetween(-5, 0, 10) = %d\n", val)
// Higher than max
val = to.ValueBetween(15, 0, 10)
fmt.Printf("ValueBetween(15, 0, 10) = %d\n", val)
}
Output: ValueBetween(5, 0, 10) = 5 ValueBetween(-5, 0, 10) = 0 ValueBetween(15, 0, 10) = 10
func ValueBetweenThe ¶ added in v0.14.0
ValueBetweenThe returns a function that clamps the value between the min and max values. In other words it ensures that result is min <= value <= max. For value that is less than min, it will return min. For value that is greater than max, it will return max. It's wrapped around ValueBetween function, to make it usable in Map functions.
See Also: ValueBetween
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Creating a percentage validator (0-100)
validatePercentage := to.ValueBetweenThe(0, 100)
fmt.Printf("validatePercentage(-20) = %d\n", validatePercentage(-20))
fmt.Printf("validatePercentage(50) = %d\n", validatePercentage(50))
fmt.Printf("validatePercentage(150) = %d\n", validatePercentage(150))
}
Output: validatePercentage(-20) = 0 validatePercentage(50) = 50 validatePercentage(150) = 100
func ValueOr ¶
func ValueOr[T any](x *T, fallback T) T
ValueOr returns the pointer value or the fallback value.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Get value from a pointer
ptr := to.Ptr(42)
val := to.ValueOr(ptr, 0)
fmt.Printf("%T(%d)\n", val, val)
// Get fallback value from a nil pointer
var nilPtr *string
strVal := to.ValueOr(nilPtr, "fallback")
fmt.Printf("%T(%q)\n", strVal, strVal)
}
Output: int(42) string("fallback")
func ValueOrGet ¶ added in v1.6.0
func ValueOrGet[T any](x *T, fallback func() T) T
ValueOrGet returns the pointer value or the fallback value from result of fallback function call.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Get value from a pointer
ptr := to.Ptr(42)
val := to.ValueOrGet(ptr, func() int { return 0 })
fmt.Printf("%T(%d)\n", val, val)
// Get fallback value from a nil pointer using fallback function
var nilPtr *string
strVal := to.ValueOrGet(nilPtr, func() string { return "computed fallback" })
fmt.Printf("%T(%q)\n", strVal, strVal)
}
Output: int(42) string("computed fallback")
func Words ¶
Words splits string into an array of its words.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Splitting into words
camelCase := to.Words("camelCaseString")
fmt.Printf("%#v\n", camelCase)
pascalCase := to.Words("PascalCaseString")
fmt.Printf("%#v\n", pascalCase)
withNumbers := to.Words("Int8Value")
fmt.Printf("%#v\n", withNumbers)
withSpaces := to.Words(" hello world ")
fmt.Printf("%#v\n", withSpaces)
}
Output: []string{"camel", "Case", "String"} []string{"Pascal", "Case", "String"} []string{"Int", "8", "Value"} []string{"hello", "world"}
func YesNo ¶ added in v1.7.0
func YesNo[T comparable](value T) string
YesNo returns "yes" if the input value is non-zero and "no" if the input value is zero.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
type SomeStruct struct {
Field string
}
// Converting various values to "yes"/"no" string
fmt.Println("empty string ->", to.YesNo(""))
fmt.Println("non-empty string ->", to.YesNo("hello"))
fmt.Println("zero ->", to.YesNo(0))
fmt.Println("positive number ->", to.YesNo(42))
fmt.Println("negative number ->", to.YesNo(-42))
fmt.Println("nil struct ->", to.YesNo((*SomeStruct)(nil)))
fmt.Println("empty struct ->", to.YesNo(SomeStruct{}))
fmt.Println("filled struct ->", to.YesNo(SomeStruct{Field: "hello"}))
}
Output: empty string -> no non-empty string -> yes zero -> no positive number -> yes negative number -> yes nil struct -> no empty struct -> no filled struct -> yes
func ZeroValue ¶
func ZeroValue[T any]() T
ZeroValue returns the zero value of type. alias: EmptyValue
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Get zero value of int
val := to.ZeroValue[int]()
fmt.Printf("%T(%d)\n", val, val)
// Get zero value of string
strVal := to.ZeroValue[string]()
fmt.Printf("%T(%q)\n", strVal, strVal)
}
Output: int(0) string("")
Types ¶
type ConvertableToNumber ¶ added in v1.2.0
ConvertableToNumber is a constraint that permits types that can be converted to number, such as strings, numbers, or booleans.
type IfElseCondition ¶
type IfElseCondition[T any] struct { // contains filtered or unexported fields }
IfElseCondition is a struct that provides a fluent API for conditional value mapping.
func If ¶
func If[T any](condition bool, resultProvider func() T) *IfElseCondition[T]
If creates a new IfElseCondition with the given condition and result provider.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Using If with function providers
result := to.If(true, func() string {
return "condition is true"
}).Else(func() string {
return "condition is false"
})
fmt.Println(result)
result = to.If(false, func() string {
return "condition is true"
}).Else(func() string {
return "condition is false"
})
fmt.Println(result)
}
Output: condition is true condition is false
func IfThen ¶
func IfThen[T any](condition bool, resultWhenTrue T) *IfElseCondition[T]
IfThen creates a new IfElseCondition with the given condition and result.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Using IfThen with direct values
result := to.IfThen(true, "condition is true").ElseThen("condition is false")
fmt.Println(result)
result = to.IfThen(false, "condition is true").ElseThen("condition is false")
fmt.Println(result)
}
Output: condition is true condition is false
func (*IfElseCondition[T]) Else ¶
func (c *IfElseCondition[T]) Else(resultProvider func() T) T
Else accepts the default result provider and returns the result of the condition evaluation.
func (*IfElseCondition[T]) ElseIf ¶
func (c *IfElseCondition[T]) ElseIf(condition bool, resultProvider func() T) *IfElseCondition[T]
ElseIf adds an else if condition.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Using ElseIf for multiple conditions
age := 25
result := to.If(age < 18, func() string {
return "minor"
}).ElseIf(age < 65, func() string {
return "adult"
}).Else(func() string {
return "senior"
})
fmt.Println(result)
// Testing with different age
age = 70
result = to.If(age < 18, func() string {
return "minor"
}).ElseIf(age < 65, func() string {
return "adult"
}).Else(func() string {
return "senior"
})
fmt.Println(result)
}
Output: adult senior
func (*IfElseCondition[T]) ElseIfThen ¶
func (c *IfElseCondition[T]) ElseIfThen(condition bool, resultWhenTrue T) *IfElseCondition[T]
ElseIfThen adds an else if condition with a result.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Using ElseIfThen for multiple conditions with direct values
score := 85
grade := to.IfThen(score >= 90, "A").
ElseIfThen(score >= 80, "B").
ElseIfThen(score >= 70, "C").
ElseIfThen(score >= 60, "D").
ElseThen("F")
fmt.Println(grade)
}
Output: B
func (*IfElseCondition[T]) ElseThen ¶
func (c *IfElseCondition[T]) ElseThen(resultWhenFalse T) T
ElseThen accepts the default result and returns the result of the condition evaluation.
type SwitchCase ¶
type SwitchCase[V comparable, R any] interface { // Case adds a case that compares equality to case value. Case(value V) SwitchThen[V, R] // When adds a case predicate function. When(func(V) bool) SwitchThen[V, R] // Default adds a default value. Default(R) R }
SwitchCase provides a fluent API for conditional (switch-like) value mapping. Represents case predicates.
func Switch ¶
func Switch[V comparable, R any](value V) SwitchCase[V, R]
Switch creates a new SwitchCase for the given value.
Example ¶
package main
import (
"fmt"
"github.com/go-softwarelab/common/pkg/to"
)
func main() {
// Using Switch with Case for equality comparisons
dayNum := 3
dayName := to.Switch[int, string](dayNum).
Case(1).ThenValue("Monday").
Case(2).ThenValue("Tuesday").
Case(3).ThenValue("Wednesday").
Case(4).ThenValue("Thursday").
Case(5).ThenValue("Friday").
Case(6).ThenValue("Saturday").
Case(7).ThenValue("Sunday").
Default("Invalid day")
fmt.Println(dayName)
}
Output: Wednesday
type SwitchThen ¶
type SwitchThen[V comparable, R any] interface { // Then adds a result provider function for given case. Then(func(V) R) SwitchCase[V, R] // ThenValue adds a result value for given case. ThenValue(R) SwitchCase[V, R] }
SwitchThen provides a fluent API for conditional (switch-like) value mapping. Represents case results.