Documentation
¶
Index ¶
- func Empty(v any) bool
- func Eq(a, b any) bool
- func False(v bool) bool
- func Gt[T cmp.Ordered](a, b T) bool
- func Gte[T cmp.Ordered](a, b T) bool
- func Lt[T cmp.Ordered](a, b T) bool
- func Lte[T cmp.Ordered](a, b T) bool
- func Neq[T comparable](a, b T) bool
- func Nil(v any) bool
- func Not(v bool) bool
- func NotEmpty(v any) bool
- func NotNil(v any) bool
- func NotSame[T comparable](a, b T) bool
- func NotZero[T comparable](v T) bool
- func Same[T comparable](a, b T) bool
- func True(v bool) bool
- func Zero[T comparable](v T) bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Empty ¶
Empty checks if the value is empty (zero value or empty string/slice/map). This function provides a comprehensive check for "empty" values across different types.
Parameters:
- v: The value to check (can be any type)
Returns:
- bool: true if the value is considered empty
Empty conditions:
- nil values
- empty strings ("")
- empty slices, maps, arrays (length 0)
- nil pointers and interfaces
- zero values for other types
Example:
is.Empty("") // true
is.Empty([]int{}) // true
is.Empty(map[string]int{}) // true
is.Empty(0) // true
is.Empty("hello") // false
is.Empty([]int{1, 2}) // false
Example ¶
ExampleIsEmpty demonstrates the IsEmpty function
package main
import (
"fmt"
"github.com/go4x/goal/is"
)
func main() {
fmt.Println(is.Empty("")) // true
fmt.Println(is.Empty([]int{})) // true
fmt.Println(is.Empty(map[string]int{})) // true
fmt.Println(is.Empty(0)) // true
fmt.Println(is.Empty("hello")) // false
fmt.Println(is.Empty([]int{1, 2})) // false
}
Output: true true true true false false
func Eq ¶
Eq checks if two values are equal. For basic types, it uses the == operator. For pointers, it compares the values they point to (not the pointer addresses). For structs and other composite types, it performs a deep comparison.
Parameters:
- a: The first value to compare
- b: The second value to compare
Returns:
- bool: true if the values are equal, false otherwise
Example:
// Basic types
is.Eq(42, 42) // true
is.Eq("hello", "hi") // false
// Pointers - compares values, not addresses
val1, val2 := 42, 42
ptr1, ptr2 := &val1, &val2
is.Eq(ptr1, ptr2) // true (values are equal, even though pointers differ)
// Structs
type Person struct {
Name string
Age int
}
p1 := Person{Name: "John", Age: 30}
p2 := Person{Name: "John", Age: 30}
is.Eq(p1, p2) // true
For functions, it uses reflect.DeepEqual, only return true if both are nil, otherwise return false Example:
type Func func() int
var v1 Func = nil
var v2 Func = nil
is.Eq(v1, v2) // true
v1 := func() int { return 1 }
v2 := v1
v3 := func() int { return 1 }
is.Eq(v1, v2) // false
is.Eq(v1, v3) // false
Example ¶
ExampleEq demonstrates the Eq function
package main
import (
"fmt"
"github.com/go4x/goal/is"
)
func main() {
fmt.Println(is.Eq(42, 42)) // true
fmt.Println(is.Eq("hello", "hi")) // false
fmt.Println(is.Eq(0, 0)) // true
slice1 := []int{1, 2, 3}
slice2 := []int{1, 2, 3}
slice3 := []int{1, 2, 4}
fmt.Println(is.Eq(slice1, slice2)) // true
fmt.Println(is.Eq(slice1, slice3)) // false
map1 := map[string]int{"a": 1, "b": 2}
map2 := map[string]int{"a": 1, "b": 2}
map3 := map[string]int{"a": 1, "b": 3}
fmt.Println(is.Eq(map1, map2)) // true
fmt.Println(is.Eq(map1, map3)) // false
// Compare structs
type Person struct {
Name string
Age int
}
p1 := Person{Name: "John", Age: 30}
p2 := Person{Name: "John", Age: 30}
p3 := Person{Name: "Jane", Age: 30}
fmt.Println(is.Eq(p1, p2)) // true
fmt.Println(is.Eq(p1, p3)) // false
type Func func() int
var v1 Func = nil
var v2 Func = nil
fmt.Println(is.Eq(v1, v2)) // true
f1 := func() int { return 1 }
f2 := f1
f3 := func() int { return 1 }
fmt.Println(is.Eq(f1, f2)) // false
fmt.Println(is.Eq(f1, f3)) // false
}
Output: true false true true false true false true false true false false
func False ¶
False returns true if the value is false.
Parameters:
- v: The boolean value to check
Returns:
- bool: true if the value is false
Example:
is.False(true) // false is.False(false) // true
Example ¶
ExampleFalse demonstrates the False function
package main
import (
"fmt"
"github.com/go4x/goal/is"
)
func main() {
fmt.Println(is.False(true)) // false
fmt.Println(is.False(false)) // true
}
Output: false true
func Gt ¶
Gt checks if a is greater than b. This function requires types that support ordering (numbers, strings, etc.).
Parameters:
- a: The first value to compare
- b: The second value to compare
Returns:
- bool: true if a > b, false otherwise
Example:
is.Gt(42, 10) // true
is.Gt("z", "a") // true
is.Gt(5, 10) // false
func Gte ¶
Gte checks if a is greater than or equal to b. This function requires types that support ordering (numbers, strings, etc.).
Parameters:
- a: The first value to compare
- b: The second value to compare
Returns:
- bool: true if a >= b, false otherwise
Example:
is.Gte(42, 42) // true is.Gte(42, 10) // true is.Gte(5, 10) // false
func Lt ¶
Lt checks if a is less than b. This function requires types that support ordering (numbers, strings, etc.).
Parameters:
- a: The first value to compare
- b: The second value to compare
Returns:
- bool: true if a < b, false otherwise
Example:
is.Lt(5, 10) // true
is.Lt("a", "z") // true
is.Lt(42, 10) // false
func Lte ¶
Lte checks if a is less than or equal to b. This function requires types that support ordering (numbers, strings, etc.).
Parameters:
- a: The first value to compare
- b: The second value to compare
Returns:
- bool: true if a <= b, false otherwise
Example:
is.Lte(42, 42) // true is.Lte(5, 10) // true is.Lte(42, 10) // false
func Neq ¶
func Neq[T comparable](a, b T) bool
Neq checks if two values are not equal. This is the logical negation of Equal.
Parameters:
- a: The first value to compare
- b: The second value to compare
Returns:
- bool: true if the values are not equal, false otherwise
Example:
is.Neq(42, 43) // true
is.Neq("hello", "hello") // false
is.Neq(0, 1) // true
func Nil ¶
Nil checks if the value is nil (for pointer, slice, map, channel, function, interface). This function uses reflection to properly check nil values for reference types.
Parameters:
- v: The value to check (can be any type)
Returns:
- bool: true if the value is nil
Example:
var ptr *int
is.Nil(ptr) // true
is.Nil(nil) // true
is.Nil([]int{}) // false (empty slice is not nil)
Example ¶
ExampleIsNil demonstrates the IsNil function
package main
import (
"fmt"
"github.com/go4x/goal/is"
)
func main() {
var ptr *int
fmt.Println(is.Nil(ptr)) // true
fmt.Println(is.Nil(nil)) // true
fmt.Println(is.Nil([]int{})) // false (empty slice is not nil)
val := 42
ptr = &val
fmt.Println(is.Nil(ptr)) // false
}
Output: true true false false
func Not ¶
Not returns the logical negation of the value. Is is an alias for False.
Parameters:
- v: The boolean value to negate
Returns:
- bool: true if the value is false, false if the value is true
Example:
is.Not(true) // false is.Not(false) // true
Example ¶
ExampleNot demonstrates the Not function
package main
import (
"fmt"
"github.com/go4x/goal/is"
)
func main() {
supported := true
if is.Not(supported) {
fmt.Println("not supported")
} else {
fmt.Println("supported")
}
}
Output: supported
func NotEmpty ¶
NotEmpty checks if the value is not empty. This is the logical negation of IsEmpty.
Parameters:
- v: The value to check (can be any type)
Returns:
- bool: true if the value is not empty
Example:
is.NotEmpty("hello") // true
is.NotEmpty([]int{1, 2}) // true
is.NotEmpty(42) // true
is.NotEmpty("") // false
is.NotEmpty([]int{}) // false
func NotNil ¶
NotNil checks if the value is not nil. This is the logical negation of IsNil.
Parameters:
- v: The value to check (can be any type)
Returns:
- bool: true if the value is not nil
Example:
ptr := &42
is.NotNil(ptr) // true
is.NotNil([]int{}) // true (empty slice is not nil)
is.NotNil(nil) // false
func NotSame ¶
func NotSame[T comparable](a, b T) bool
NotSame checks if two values are not the same. This is the logical negation of Same.
Parameters:
- a: The first value to compare
- b: The second value to compare
Returns:
- bool: true if the values are not the same, false otherwise
Example:
is.NotSame(42, 43) // true
is.NotSame("hello", "hello") // false
is.NotSame(0, 1) // true
func NotZero ¶
func NotZero[T comparable](v T) bool
NotZero checks if the value is not the zero value for its type. This is the logical negation of IsZero.
Parameters:
- v: The value to check
Returns:
- bool: true if the value is not the zero value for its type
Example:
is.NotZero(42) // true
is.NotZero("hello") // true
is.NotZero(0) // false
is.NotZero("") // false
func Same ¶
func Same[T comparable](a, b T) bool
Same checks if two values are the same. This is an alias for Eq, using the == operator for comparison. Same emphasizes that the values are identical, not just equal.
Parameters:
- a: The first value to compare
- b: The second value to compare
Returns:
- bool: true if the values are the same, false otherwise
Example:
is.Same(42, 42) // true
is.Same("hello", "hi") // false
is.Same(0, 0) // true
var ptr1, ptr2 *int
val := 42
ptr1 = &val
ptr2 = &val
is.Same(ptr1, ptr2) // true (same pointer)
func True ¶
True returns true if the value is true.
Parameters:
- v: The boolean value to check
Returns:
- bool: true if the value is true
Example:
is.True(true) // true is.True(false) // false
func Zero ¶
func Zero[T comparable](v T) bool
Zero checks if the value is the zero value for its type. Zero values are: 0 for numeric types, "" for strings, false for bools, nil for pointers/slices/maps/channels/functions/interfaces.
Parameters:
- v: The value to check
Returns:
- bool: true if the value is the zero value for its type
Example:
is.Zero(0) // true
is.Zero("") // true
is.Zero(false) // true
is.Zero(42) // false
is.Zero("hello") // false
Example ¶
ExampleIsZero demonstrates the IsZero function
package main
import (
"fmt"
"github.com/go4x/goal/is"
)
func main() {
fmt.Println(is.Zero(0)) // true
fmt.Println(is.Zero("")) // true
fmt.Println(is.Zero(false)) // true
fmt.Println(is.Zero(42)) // false
fmt.Println(is.Zero("hello")) // false
}
Output: true true true false false
Types ¶
This section is empty.