Documentation
¶
Overview ¶
Package gvalue provides generic operations for go values.
💡 HINT: We provide similar functionality for different types in different packages. For example, github.com/bytedance/gg/gslice.Clone for copying slice while github.com/bytedance/gg/gmap.Clone for copying map.
- Use github.com/bytedance/gg/gslice for slice operations.
- Use github.com/bytedance/gg/gmap for map operations.
- Use github.com/bytedance/gg/gptr for pointer operations.
- …
Operations ¶
- Math operations: Max, Min, MinMax, Clamp, …
- Type assertion (T1 → T2): TypeAssert, TryAssert, …
- Predicate: (T → bool): Equal, Greater, Less, Between, IsNil, IsZero, …
Example ¶
// Zero value a := Zero[int]() fmt.Println(a) // 0 fmt.Println(IsZero(a)) // true b := Zero[*int]() fmt.Println(b) // nil fmt.Println(IsNil(b)) // true fmt.Println(Or(0, 1, 2)) // 1 // Math operation fmt.Println(Max(1, 2, 3)) // 3 fmt.Println(Min(1, 2, 3)) // 1 fmt.Println(MinMax(1, 2, 3)) // 1 3 fmt.Println(Clamp(5, 1, 10)) // 5 fmt.Println(Add(1, 2)) // 3 // Comparison fmt.Println(Equal(1, 1)) // true fmt.Println(Between(2, 1, 3)) // true // Type assertion fmt.Println(TypeAssert[int](any(1))) // 1 fmt.Println(TryAssert[int](any(1))) // 1 true
Output: 0 true <nil> true 1 3 1 1 3 5 3 true true 1 1 true
Index ¶
- func Add[T constraints.Number | constraints.Complex | ~string](x, y T) T
- func Between[T constraints.Ordered](v, min, max T) bool
- func Clamp[T constraints.Ordered](value, min, max T) T
- func Equal[T comparable](x, y T) bool
- func Greater[T constraints.Ordered](x, y T) bool
- func GreaterEqual[T constraints.Ordered](x, y T) bool
- func IsNil(v any) bool
- func IsNotNil(v any) bool
- func IsNotZero[T comparable](v T) bool
- func IsZero[T comparable](v T) bool
- func Less[T constraints.Ordered](x, y T) bool
- func LessEqual[T constraints.Ordered](x, y T) bool
- func Max[T constraints.Ordered](x T, y ...T) T
- func Min[T constraints.Ordered](x T, y ...T) T
- func MinMax[T constraints.Ordered](x T, y ...T) (T, T)
- func Or[T comparable](vals ...T) (v T)
- func TryAssert[To, From any](v From) (To, bool)
- func TypeAssert[To, From any](v From) To
- func Zero[T any]() (v T)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
func Add[T constraints.Number | constraints.Complex | ~string](x, y T) T
Add adds given values x and y and returns the sum. For string, Add performs concatenation.
func Between ¶
func Between[T constraints.Ordered](v, min, max T) bool
Between returns true when v is within [min, max], otherwise false.
func Clamp ¶
func Clamp[T constraints.Ordered](value, min, max T) T
Clamp returns the value if the value is within [min, max]; otherwise returns the nearest boundary. If min is greater than max, the behavior is undefined.
🚀 EXAMPLE:
Clamp(1, 2, 3) ⏩ 2
Clamp(2, 1, 3) ⏩ 2
Clamp(3, 1, 2) ⏩ 2
Clamp("2", "10", "11") ⏩ "11"
func Equal ¶
func Equal[T comparable](x, y T) bool
Equal returns whether the given x and y are equal.
func Greater ¶
func Greater[T constraints.Ordered](x, y T) bool
Greater returns true when x is greater than y, otherwise false.
func GreaterEqual ¶
func GreaterEqual[T constraints.Ordered](x, y T) bool
GreaterEqual returns true when x is greater than or equal to y, otherwise false.
func IsNil ¶
IsNil returns whether the given value v is nil.
💡 NOTE: Typed nil interface (such as fmt.Stringer((*net.IP)(nil))) is nil, although fmt.Stringer((*net.IP)(nil)) != nil.
🚀 EXAMPLE:
IsNil(nil) ⏩ true IsNil(1) ⏩ false IsNil((*int)(nil)) ⏩ true IsNil(fmt.Stringer((*net.IP)(nil))) ⏩ true
func IsZero ¶
func IsZero[T comparable](v T) bool
IsZero returns whether the given v is zero value.
💡 HINT: Refer to function Zero for explanation of zero value.
func Less ¶
func Less[T constraints.Ordered](x, y T) bool
Less returns true when x is less than y, otherwise false.
func LessEqual ¶
func LessEqual[T constraints.Ordered](x, y T) bool
LessEqual returns true when x is less than or equal to y, otherwise false.
func Max ¶
func Max[T constraints.Ordered](x T, y ...T) T
Max returns the maximum value of inputs.
🚀 EXAMPLE:
Max(1, 2) ⏩ 2
Max(1, 2, 3) ⏩ 3
Max("2", "10", "11") ⏩ "2"
func Min ¶
func Min[T constraints.Ordered](x T, y ...T) T
Min returns the minimum value of inputs.
🚀 EXAMPLE:
Min(1, 2) ⏩ 1
Min(1, 2, 3) ⏩ 1
Min("2", "10", "11") ⏩ "10"
func MinMax ¶
func MinMax[T constraints.Ordered](x T, y ...T) (T, T)
MinMax returns the minimum value and maximum value of inputs.
🚀 EXAMPLE:
MinMax(1, 2) ⏩ 1, 2
MinMax(1, 2, 3) ⏩ 1, 3
MinMax("2", "10", "11") ⏩ "10", "2"
func Or ¶
func Or[T comparable](vals ...T) (v T)
Or returns the first non-zero value of inputs. If all values are zero, return the zero value of type.
🚀 EXAMPLE:
Or(false, true) ⏩ true
Or(0, 1, 2) ⏩ 1
Or("", "1", "2") ⏩ "1"
Or(0, 0, 0) ⏩ 0
Or("", "", "") ⏩ ""
func TypeAssert ¶
func TypeAssert[To, From any](v From) To
TypeAssert converts a value from type From to type To by type assertion.
⚠️ WARNING: *Type assertion* is not type conversion/casting, it means that:
- It may ❌PANIC❌ when type assertion failed
- You can NOT cast int values to int8, can NOT cast int value to string
- You can cast interface value to int if its internal value is an int
💡 NOTE: The first type parameter is result type (To), which means you can omit the original type (From) via type inference.
Types ¶
This section is empty.