Documentation ¶
Index ¶
- Variables
- func CloneMap[K comparable, V any](src map[K]V) map[K]V
- func Conditional[T any](cond bool, trueExpr, falseExpr T) T
- func ConditionalExpr[T any](cond bool, trueExpr, falseExpr func() T) T
- func CopyMap[K comparable, V any](dest, src map[K]V) error
- func GetElem(o any) any
- func IsComparableType(v any) bool
- func IsSameRawType(v1, v2 any) bool
- func IsSameType(v1, v2 any) bool
- func Matrix[T any](rows, columns int, values ...T) [][]T
- func Max[T constraints.Ordered](v1, v2 T) T
- func MaxN[T constraints.Ordered](values ...T) T
- func Min[T constraints.Ordered](v1, v2 T) T
- func MinN[T constraints.Ordered](values ...T) T
- func Pointer[T any](v T) *T
- func PointerMap[K comparable, V any](src map[K]V) map[K]*V
- func PointerSlice[T any](src []T) []*T
- func PointerWithDefault[T any](v *T, defaultValue *T) *T
- func RawTypeOf(v any) string
- func Try(fn func() error) (err error)
- func TryCatch(tryStmt func() error, catchStmt func(error), finallyStmt ...func()) error
- func TypeOf(v any) string
- func Value[T any](v *T) T
- func ValueMap[K comparable, V any](src map[K]*V) map[K]V
- func ValueSlice[T any](src []*T) []T
- func ValueWithDefault[T any](val *T, defaultVal T) T
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func CloneMap ¶ added in v0.1.3
func CloneMap[K comparable, V any](src map[K]V) map[K]V
CloneMap creates a new map, and copies all the keys and their value from the source map into the new map. It will return nil if the source map is nil.
oldMap := map[string]int{ "a": 1, "b": 2 } newMap := CloneMap(oldMap) log.Print(newMap) // [a:1 b:2]
func Conditional ¶
Conditional is an alternative to the conditional (ternary) operator (?:).
a := 1 b := 2 Conditional(a > b, a, b) // 2
func ConditionalExpr ¶ added in v0.1.1
ConditionalExpr is an alternative to the conditional (ternary) operator (?:), it'll run expression by the conditional result.
var strp *string // nil ConditionalExpr(strp == nil, func() string { return "" }, func() string { return *strp }, ) // "" strp = Pointer("hello") ConditionalExpr(strp == nil, func() string { return "" }, func() string { return *strp }, ) // "hello"
func CopyMap ¶ added in v0.1.3
func CopyMap[K comparable, V any](dest, src map[K]V) error
CopyMap copies all keys and their value from the source map into the destination map, and it will overwrite the old value in the destination map if the key has existed. The function will return a nil destination error if the source map is not nil and the destination map is nil.
src := map[string]int{ "a": 1, "b": 2 } dest := map[string]int{ "a": 10, "c": 3 } CopyMap(dest, src) log.Print(dest) // [a:1 b:2 c:3]
func GetElem ¶
GetElem returns the element without pointer.
v := 1 vp := &v GetElem(v) // 1 GetElem(vp) // 1
func IsComparableType ¶ added in v0.2.0
IsComparableType gets the value type and checks whether the type is comparable or not.
IsComparableType(1) // true for int IsComparableType(1.0) // true for float64 IsComparableType("") // true for string IsComparableType(nil) // false for nil IsComparableType(SomeStruct{}) // false for struct IsComparableType(&SomeStruct{}) // false for pointer
func IsSameRawType ¶
IsSameRawType compares two values' type without pointer.
v1 := 1 // int v2 := 2 // int v3 := Pointer(3) // *int IsSameRawType(v1, v2) // true IsSameRawType(v1, v3) // true
func IsSameType ¶
IsSameType compares two values' type.
v1 := 1 // int v2 := 2 // int v3 := Pointer(3) // *int IsSameType(v1, v2) // true IsSameType(v1, v3) // false
func Matrix ¶ added in v0.2.0
Matrix creates a rows * columns matrix, and initialize it with the values.
Matrix[int](2, 2) // [][]int{{0, 0}, {0, 0}} Matrix(2, 2, 1, 2, 3, 4) // [][]int{{1, 2}, {3, 4}} Matrix(2, 2, 1, 2, 3) // [][]int{{1, 2}, {3, 0}}
func Max ¶
func Max[T constraints.Ordered](v1, v2 T) T
Max returns the maximum value between v1 and v2.
Max(1, 2) // 2 Max(1.0, 2.0) // 2.0 Max("str1", "str2") // "str2"
func MaxN ¶ added in v0.2.0
func MaxN[T constraints.Ordered](values ...T) T
`MaxN` returns the maximum value in the list and returns the zero value of the type if no parameter.
MaxN[int]() // 0 MaxN(2, 1) // 2 MaxN(3, 2, 1, 0, 2, 4) // 4
func Min ¶
func Min[T constraints.Ordered](v1, v2 T) T
Min returns the minimum value between v1 and v2.
Min(1, 2) // 1 Min(1.0, 2.0) // 1.0 Min("str1", "str2") // "str1"
func MinN ¶ added in v0.2.0
func MinN[T constraints.Ordered](values ...T) T
`MinN` returns the minimum value in the list and returns the zero value of the type if no parameter.
MinN[int]() // 0 MinN(1, 2) // 1 MinN(3, 2, 1, 0, 2, 4) // 0
func Pointer ¶
func Pointer[T any](v T) *T
Pointer returns a pointer to the value passed in.
v := 1 // type is int vp := Pointer(v) // type is *int, *vp = 1 TypeOf(vp) // "*int"
func PointerMap ¶
func PointerMap[K comparable, V any](src map[K]V) map[K]*V
PointerMap converts a map of values into a map of pointers.
func PointerSlice ¶
func PointerSlice[T any](src []T) []*T
PointerToSlice converts a slice of values into a slice of pointers.
func PointerWithDefault ¶ added in v0.2.0
func PointerWithDefault[T any](v *T, defaultValue *T) *T
PointerWithDefault returns the pointer if it is not nil, or the default pointer.
v := Pointer(1) dv := Pointer(2) PointerWithDefault(v, dv) // &1 v = nil PointerWithDefault(v, dv) // &2
func RawTypeOf ¶
RawTypeOf returns the type string name without pointer.
RawTypeOf(nil) // "<nil>" RawTypeOf(1) // "int" RawTypeOf("test") // "string" RawTypeOf(&http.Client{}) // "http.Client"
func Try ¶ added in v0.2.0
Try provided a recoverable function container to handle both return error and panic error.
Try(func() error { panic("some error") }) // some error
func TryCatch ¶ added in v0.2.0
TryCatch is an alternative to the `try...catch...finally` statement. This function requires a `try` statement function and a `catch` statement function, and it accepts an optional `finally` statement function.
This function will execute the try statement function, and call the catch statement function if the try function returns an error or panic. It will always perform the `finally` statement function whether the try function is successful or not if the `finally` function is not nil. However, if the `catch` function panics, this TryCatch statement will also panic, and finally function will never run.
err := TryCatch( func() error { // Do something as try statement return nil }, func (err error) { // Do something as catch statement }, func () { // An optional finally statement }, )
func TypeOf ¶ added in v0.1.1
TypeOf returns the type of the value represented in string.
TypeOf(nil) // "<nil>" TypeOf(1) // "int" TypeOf("test") // "string" TypeOf(&http.Client{}) // "*http.Client"
func Value ¶
func Value[T any](v *T) T
Value returns the value of a pointer, or the zero value of the type if the pointer is nil.
var p1 *string // nil var p2 *string = Pointer("hello") Value(p1) // "" Value(p2) // "hello"
func ValueMap ¶
func ValueMap[K comparable, V any](src map[K]*V) map[K]V
ValueMap converts a map of pointers into a map of values, and it'll set the value to the zero value of the type if the pointer is point to nil.
m1 := map[string]*int{ "a": Pointer(1), "b": nil, "c": Pointer(3) } m2 := ValueMap(m1) // [a:1, b:0, c:3]
func ValueSlice ¶
func ValueSlice[T any](src []*T) []T
ValueSlice converts a slice of pointers into a slice of values, and it'll set the value to the zero value of the type if the pointer is point to nil.
s1 := []*string{Pointer("Hello"), nil, Pointer("World")} s2 := ValueSlice(s1) // ["Hello", "", "World"]
func ValueWithDefault ¶
func ValueWithDefault[T any](val *T, defaultVal T) T
ValueWithDefault returns the value passed in if it is not nil, otherwise returns the default value.
var p1 *string // nil var p2 *string = Pointer("hello") ValueWithDefault(p1, "default") // "default" ValueWithDefault(p2, "default") // "hello"
Types ¶
This section is empty.