Documentation ¶
Overview ¶
Package op provides a set of generic functions that extend and complement Go's built-in operators and basic operations. It includes utilities for conditional logic, comparisons, and type manipulations.
Index ¶
- func Addr[T any](x T) *T
- func Assert(cond bool, msgs ...any)
- func Assertf(cond bool, format string, args ...any)
- func Bin[T comparable](x T) int
- func Deref[T any](p *T) T
- func DerefOr[T any](p *T, x T) T
- func DerefOrFunc[T any](p *T, x func() T) T
- func First[T any](first T, _ ...any) T
- func Identity[T any](v T) func() T
- func If[T any](condition bool, a, b T) T
- func IfFunc[T any](condition bool, a T, b func() T) T
- func IfFunc2[T any](condition bool, a, b func() T) T
- func Must(err error)
- func MustResult[T any](value T, err error) T
- func MustResult2[T1, T2 any](value1 T1, value2 T2, err error) (T1, T2)
- func Or[T comparable](a, b T) T
- func OrFunc[T comparable](a T, b func() T) T
- func Result(value any, err error) any
- func ReverseCompare[T any](cmp func(T, T) int) func(T, T) int
- func Second[T1, T2 any](first T1, second T2, _ ...any) T2
- func SetDefault[T comparable](a *T, b T) T
- func SetDefaultFunc[T comparable](a *T, b func() T) T
- func Third[T1, T2, T3 any](first T1, second T2, third T3, _ ...any) T3
- func Zero[T any]() T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bin ¶
func Bin[T comparable](x T) int
Bin converts a comparable value to a binary number (0 or 1). It returns 0 if the input is equal to its zero value, and 1 otherwise.
func Deref ¶
func Deref[T any](p *T) T
Deref returns the value of p if it is not nil, otherwise it returns the zero value of T.
func DerefOr ¶
func DerefOr[T any](p *T, x T) T
DerefOr returns the value of p if it is not nil, otherwise it returns x.
func DerefOrFunc ¶
func DerefOrFunc[T any](p *T, x func() T) T
DerefOr returns the value of p if it is not nil, otherwise it returns result of calling x().
func Identity ¶
func Identity[T any](v T) func() T
Identity returns a function that returns the input value.
func If ¶
If returns a if condition is true, otherwise returns b. It provides a generic ternary operation for any type.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/op" ) func main() { condition := true fmt.Println(op.If(condition, "It's true", "It's false")) condition = false fmt.Println(op.If(condition, "It's true", "It's false")) }
Output: It's true It's false
func IfFunc2 ¶
IfFunc2 returns the result of calling a() if condition is true, otherwise returns the result of calling b(). It allows for lazy evaluation of both alternatives.
func MustResult ¶
MustResult panics if err is not nil, otherwise it returns value. It is a convenient way to handle errors in a single line.
func MustResult2 ¶
MustResult2 panics if err is not nil, otherwise it returns value1 and value2.
func Or ¶
func Or[T comparable](a, b T) T
Or returns b if a is the zero value for T, otherwise returns a.
Example ¶
package main import ( "fmt" "github.com/gopherd/core/op" ) func main() { fmt.Println(op.Or(0, 1)) fmt.Println(op.Or(2, 3)) fmt.Println(op.Or("", "default")) fmt.Println(op.Or("value", "default")) }
Output: 1 2 default value
Example (Sort) ¶
package main import ( "fmt" "slices" "github.com/gopherd/core/op" ) func main() { type Order struct { Product string Customer string Price float64 } orders := []Order{ {"foo", "alice", 1.00}, {"bar", "bob", 3.00}, {"baz", "carol", 4.00}, {"foo", "alice", 2.00}, {"bar", "carol", 1.00}, {"foo", "bob", 4.00}, } // Sort by customer first, product second, and last by higher price slices.SortFunc(orders, func(a, b Order) int { customerCmp := op.If(a.Customer < b.Customer, -1, op.If(a.Customer > b.Customer, 1, 0)) productCmp := op.If(a.Product < b.Product, -1, op.If(a.Product > b.Product, 1, 0)) priceCmp := op.If(b.Price < a.Price, -1, op.If(b.Price > a.Price, 1, 0)) return op.Or(op.Or(customerCmp, productCmp), priceCmp) }) for _, order := range orders { fmt.Printf("%s %s %.2f\n", order.Customer, order.Product, order.Price) } }
Output: alice foo 2.00 alice foo 1.00 bob bar 3.00 bob foo 4.00 carol bar 1.00 carol baz 4.00
func OrFunc ¶
func OrFunc[T comparable](a T, b func() T) T
OrFunc returns the result of calling b() if a is the zero value for T, otherwise returns a. It allows for lazy evaluation of the alternative value.
func ReverseCompare ¶
ReverseCompare returns a comparison function that reverses the order of the original comparison function.
func Second ¶
Second returns the second argument. It extracts the second value from a set of arguments.
func SetDefault ¶
func SetDefault[T comparable](a *T, b T) T
SetDefault sets the value of a to b if a is the zero value for T. It returns the final value of a.
func SetDefaultFunc ¶
func SetDefaultFunc[T comparable](a *T, b func() T) T
SetDefaultFunc sets the value of a to the result of calling b() if a is the zero value for T. It returns the final value of a.
Types ¶
This section is empty.