nullorempty

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2025 License: MIT Imports: 1 Imported by: 0

README

nullorempty

The nullorempty package provides a utility function to check if a given value is nil or empty. This function can handle various types like nil, string, slice, map, array, and pointer, and determines whether they are "empty" based on their type.

Installation

To use the nullorempty package, simply import it into your Go project:

package main

import (
    "fmt"
    "github.com/inuoshios/nullorempty"
)

func main() {
    fmt.Println(nullorempty.IsNullOrEmpty(nil))         // true
    fmt.Println(nullorempty.IsNullOrEmpty(""))          // true
    fmt.Println(nullorempty.IsNullOrEmpty("Hello"))     // false
    fmt.Println(nullorempty.IsNullOrEmpty([]int{}))     // true
    fmt.Println(nullorempty.IsNullOrEmpty(map[string]int{})) // true
    fmt.Println(nullorempty.IsNullOrEmpty([]int{1, 2})) // false
}

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Please feel free to contribute to this project by submitting pull requests or opening issues. Your contributions are highly appreciated!

Authors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsNullOrEmpty

func IsNullOrEmpty(val any) bool

IsNullOrEmpty checks if the provided value is nil or empty. It returns true if the value is nil, an empty string, or an empty slice (including arrays and maps). It returns false otherwise.

Supported types: - nil: Always returns true. - string: Returns true if the string is empty (""). - slice: Returns true if the slice has no elements. - map: Returns true if the map has no elements. - array: Returns true if the array has no elements. - pointer: Returns true if the pointer is nil.

Example usage:

package main

import (
    "fmt"
    "github.com/inuoshios/nullorempty"
)

func main() {
    fmt.Println(nullorempty.IsNullOrEmpty(nil))         // true
    fmt.Println(nullorempty.IsNullOrEmpty(""))          // true
    fmt.Println(nullorempty.IsNullOrEmpty("Hello"))     // false
    fmt.Println(nullorempty.IsNullOrEmpty([]int{}))     // true
    fmt.Println(nullorempty.IsNullOrEmpty(map[string]int{})) // true
    fmt.Println(nullorempty.IsNullOrEmpty([]int{1, 2})) // false
}

Returns: - bool: true if the value is nil or empty, false otherwise.

Types

This section is empty.

Jump to

Keyboard shortcuts

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