Simple every day type conversion tooling.
Thanks to modern golang and generics, you can say now:
pointerToOne = tc.P(1)
instead of using tons of typed casters like this.
There are different variants for this function: nil pointer for zero values, defaults... take a look at documentation
There are functions to cat any
to any type with or without fallback to default. Like this:
x := tc.DefCast(ctx.Value("key"), "default") // x = "default" if there is no "key" in context
or without default
x := tc.SafeCast[int](ctx.Value("key")) // x = 0 if there is no "key" in context
Package provides sort of ternary operator:
// useful for globals
var messagePrefix = tc.Cmp(os.GetEnv("APP_ENVIRONMENT") == "prod", "", "[FROM STAGING] ")
and extended operator that deals with *bool
, considering &true
, &false
and nil
:
user := struct {
Male *bool
}{}
gender := tc.CmpN(user.Male, "male", "female", "n/a")
You can simply assume defaults for zero values like this:
var tmpDir = tc.DefZero(os.GetEnv("TMP"), "/tmp")