Documentation
¶
Overview ¶
Package constraints provides type constraints for generic programming in Go.
It defines a set of interface types that can be used as type constraints in generic functions and types. These constraints cover various numeric types and their combinations, allowing for more precise and flexible generic programming with numbers in Go.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Addable ¶
Addable is a constraint that permits any number or string type.
Example ¶
package main
import (
"fmt"
"github.com/gopherd/core/constraints"
)
func sum[T constraints.Addable](values ...T) T {
var result T
for _, v := range values {
result += v
}
return result
}
func main() {
fmt.Println(sum(1, 2))
fmt.Println(sum("hello", " ", "world"))
}
Output: 3 hello world
type Complex ¶
type Complex interface {
~complex64 | ~complex128
}
Complex is a constraint that permits any complex numeric type.
type SignedNumber ¶
type SignedNumber interface {
SignedReal | Complex
}
SignedNumber is a constraint that permits any signed numeric type.
type SignedReal ¶
SignedReal is a constraint that permits any signed real number type.
Click to show internal directories.
Click to hide internal directories.