Utilities for the Golang package. The utils package is a generic package that provides utility functions and structures that can be used across different parts of a program. The package contains several functions and structures that can be used to simplify common tasks.
go get github.com/dollarsignteam/go-utils
PointerOf
returns a pointer to the input value. For example:
x := 42
ptr := PointerOf(x)
fmt.Println(*ptr) // Output: 42
PackageName
returns the name of the package that calls it. For example:
fmt.Println(PackageName()) // Output: utils
UniqueOf
removes duplicates from a slice of any type and returns a new slice containing only the unique elements. For example:
input := []int{1, 2, 3, 2, 1}
unique := UniqueOf(input)
fmt.Println(unique) // Output: [1 2 3]
ValueOf
takes a pointer to a value of any type and returns the value. For example:
x := 42
ptr := &x
val := ValueOf(ptr)
fmt.Println(val) // Output: 42
IsArrayOrSlice
takes a value of any type and returns a boolean indicating if it is a slice or an array. For example:
arr := [3]int{1, 2, 3}
slice := []int{1, 2, 3}
fmt.Println(IsArrayOrSlice(arr)) // Output: true
fmt.Println(IsArrayOrSlice(slice)) // Output: true
fmt.Println(IsArrayOrSlice(x)) // Output: false
BoolToInt
converts a boolean value to an integer (1 for true, 0 for false). For example:
fmt.Println(BoolToInt(true)) // Output: 1
fmt.Println(BoolToInt(false)) // Output: 0
IntToBool
converts an integer value to a boolean (true for non-zero values, false for zero). For example:
fmt.Println(IntToBool(1)) // Output: true
fmt.Println(IntToBool(0)) // Output: false
For more information, check out the 📚 documentation.
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Dollarsign
Licensed under the MIT License - see the LICENSE file for details.