Documentation
¶
Overview ¶
Package truthy has boolean tests of truthiness and helper functions.
Index ¶
- func Coalesce[T any](p *T, v T) T
- func Cond[T any](cond bool, ifVal, elseVal T) T
- func Deref[T any](p *T) T
- func First[T comparable](vs ...T) (t T)
- func FirstAny[T any](vs ...T) (t T)
- func SetDefault[T comparable](p *T, defaults ...T)
- func SetDefaultAny[T any](p *T, defaults ...T)
- func Value[T comparable](v T) bool
- func ValueAny[T any](v T) bool
- func ValueMap[K comparable, V any, M ~map[K]V](v M) bool
- func ValueSlice[T any, S ~[]T](v S) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Coalesce ¶ added in v0.23.1
func Coalesce[T any](p *T, v T) T
Coalesce returns *p if p is not nil, otherwise v.
Example ¶
package main
import (
"fmt"
"github.com/carlmjohnson/truthy"
)
func main() {
var np *int
fmt.Println(truthy.Coalesce(np, 1))
np = new(int)
fmt.Println(truthy.Coalesce(np, 1))
}
Output: 1 0
func Cond ¶
Cond returns ifVal if cond is true, otherwise it returns elseVal.
Example (Lazy) ¶
package main
import (
"fmt"
"github.com/carlmjohnson/truthy"
)
func main() {
i := 1
// Cond cannot lazily evaluate its arguments,
// but you can use a closure to fake it.
s := truthy.Cond(
truthy.Value(i),
func() string {
// do some calculation
return "true"
},
func() string {
// do some calculation
return "false"
})()
fmt.Printf("%q\n", s)
}
Output: "true"
func Deref ¶ added in v0.23.1
func Deref[T any](p *T) T
Deref returns *p if p is not nil, otherwise the zero value of T.
Example ¶
package main
import (
"fmt"
"github.com/carlmjohnson/truthy"
)
func main() {
var np *int
fmt.Println(truthy.Deref(np))
one := 1
np = &one
fmt.Println(truthy.Deref(np))
}
Output: 0 1
func First ¶
func First[T comparable](vs ...T) (t T)
First returns the first value in vs which is non-zero.
Example ¶
package main
import (
"fmt"
"time"
"github.com/carlmjohnson/truthy"
)
type MyStruct struct {
Port int
Host string
Timeout time.Duration
}
func MakeMyStruct(port int, host string, timeout time.Duration) *MyStruct {
return &MyStruct{
Port: truthy.First(port, 80),
Host: truthy.First(host, "localhost"),
Timeout: truthy.First(timeout, 10*time.Second),
}
}
func main() {
fmt.Println(MakeMyStruct(0, "", 0))
fmt.Println(MakeMyStruct(443, "example.com", 1*time.Minute))
}
Output: &{80 localhost 10s} &{443 example.com 1m0s}
func FirstAny ¶ added in v0.23.1
func FirstAny[T any](vs ...T) (t T)
FirstAny returns the first value in vs which is truthy.
func SetDefault ¶
func SetDefault[T comparable](p *T, defaults ...T)
SetDefault sets p to the first non-zero value in defaults if it is not already non-zero.
Example ¶
package main
import (
"fmt"
"time"
"github.com/carlmjohnson/truthy"
)
type MyStruct struct {
Port int
Host string
Timeout time.Duration
}
func NewMyStruct(port int, host string, timeout time.Duration) *MyStruct {
s := MyStruct{port, host, timeout}
truthy.SetDefault(&s.Port, 80)
truthy.SetDefault(&s.Host, "localhost")
truthy.SetDefault(&s.Timeout, 10*time.Second)
return &s
}
func main() {
fmt.Println(NewMyStruct(0, "", 0))
fmt.Println(NewMyStruct(443, "example.com", 1*time.Minute))
}
Output: &{80 localhost 10s} &{443 example.com 1m0s}
func SetDefaultAny ¶ added in v0.23.1
func SetDefaultAny[T any](p *T, defaults ...T)
SetDefaultAny sets p to the first truthy value in defaults if it is not already truthy.
func Value ¶
func Value[T comparable](v T) bool
Value returns the truthy value of comparable types. Values are truthy if they are not equal to the zero value for the type.
Example ¶
package main
import (
"errors"
"fmt"
"time"
"github.com/carlmjohnson/truthy"
)
func main() {
var err error
fmt.Println(truthy.Value(err))
err = errors.New("hi")
fmt.Println(truthy.Value(err))
var n int
fmt.Println(truthy.Value(n))
n = 1
fmt.Println(truthy.Value(n))
var p *int
fmt.Println(truthy.Value(p))
p = new(int)
// truthy does not check value underlying pointer!
fmt.Println(truthy.Value(p))
var s string
fmt.Println(truthy.Value(s))
s = " "
fmt.Println(truthy.Value(s))
var b []byte
fmt.Println(truthy.ValueSlice(b))
b = []byte(" ")
fmt.Println(truthy.ValueSlice(b))
m := map[string]string{}
fmt.Println(truthy.ValueMap(m))
m["a"] = "b"
fmt.Println(truthy.ValueMap(m))
var t time.Time
t = t.Local()
// t.IsZero() is still true although t is not the empty value
fmt.Println(truthy.ValueAny(t))
t = t.Add(1 * time.Second)
fmt.Println(truthy.ValueAny(t))
}
Output: false true false true false true false true false true false true false true
func ValueAny ¶ added in v0.23.1
ValueAny returns the truthy value of anything. If the value's type has a Bool() bool method, the method is called and returned. If the type has an IsZero() bool method, the opposite value is returned. Slices and maps are truthy if they have a length greater than zero. All other types are truthy if they are not their zero value.
Note that the usual Go type system caveats apply around a nil pointer value not being a nil interface value.
In benchmark testing, ValueAny is approximately 25 times slower than Value, and 50 times slower than native Go comparisons.
func ValueMap ¶ added in v0.23.1
func ValueMap[K comparable, V any, M ~map[K]V](v M) bool
ValueMap returns true if the length of the map is greater than 0. Note that it does not distinguish nil maps from empty maps.
func ValueSlice ¶ added in v0.23.1
ValueSlice returns true if the length of the slice is greater than 0. Note that it does not distinguish nil slices from empty slices.
Types ¶
This section is empty.
