Documentation ¶
Index ¶
- func All[T any](vs ...T) bool
- func And[T, U any](a T, b U) bool
- func Any[T any](vs ...T) bool
- func Cond[T, U any](cond T, ifVal, elseVal U) U
- func Filter[T any](vs *[]T)
- func FilterFunc[T, U any](vs *[]T, keep func(T) U)
- func First[T any](vs ...T) (t T)
- func Nand[T, U any](a T, b U) bool
- func Nor[T, U any](a T, b U) bool
- func Or[T, U any](a T, b U) bool
- func SetDefault[T any](p *T, defaults ...T)
- func Value[T any](v T) bool
- func Xnor[T, U any](a T, b U) bool
- func Xor[T, U any](a T, b U) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Cond ¶
func Cond[T, U any](cond T, ifVal, elseVal U) U
Cond returns ifVal if cond is truthy, otherwise it returns elseVal.
Note that Cond cannot do lazy evaluation of its arguments.
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(i, func() string { // do some calculation return "true" }, func() string { // do some calculation return "false" })() fmt.Printf("%q\n", s) }
Output: "true"
func FilterFunc ¶
func FilterFunc[T, U any](vs *[]T, keep func(T) U)
func First ¶
func First[T any](vs ...T) (t T)
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 SetDefault ¶
func SetDefault[T any](p *T, defaults ...T)
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 Value ¶
Value 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.
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)) m := map[string]string{} fmt.Println(truthy.Value(m)) m["a"] = "b" fmt.Println(truthy.Value(m)) var t time.Time t = t.Local() // t.IsZero() is still true although t is not the empty value fmt.Println(truthy.Value(t)) t = t.Add(1 * time.Second) fmt.Println(truthy.Value(t)) }
Output: false true false true false true false true false true false true
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.