gvalue

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 4, 2025 License: Apache-2.0 Imports: 2 Imported by: 6

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.

Operations

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

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

func IsNil(v any) bool

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 IsNotNil

func IsNotNil(v any) bool

IsNotNil is negation of IsNil.

func IsNotZero

func IsNotZero[T comparable](v T) bool

IsNotZero is negation of IsZero.

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 TryAssert

func TryAssert[To, From any](v From) (To, bool)

TryAssert tries to convert a value from type From to type To by [type assertion].

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:

  1. It may ❌PANIC❌ when type assertion failed
  2. You can NOT cast int values to int8, can NOT cast int value to string
  3. 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.

func Zero

func Zero[T any]() (v T)

Zero returns zero value of type.

The zero value is:

  • 0 for numeric types,
  • false for the boolean type
  • "" (the empty string) for strings
  • nil for reference/pointer type

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL