utils

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2022 License: MIT Imports: 2 Imported by: 2

README

Utilities for Golang

Test codecov Latest version License Badge Go Reference

A set of utilities functions for Golang.

PLEASE NOTE: This package is working with Go 1.18 and later versions.

Installation

You can install this package with go get command:

go get -u github.com/ghosind/utils

APIs

Conditional
  • Conditional[T any](cond bool, trueExpr, falseExpr T) T

    An alternative of ternary operator, same of cond ? trueExpr : falseExpr.

  • ConditionalExpr[T any](cond bool, trueExpr, falseExpr func() T) T

    An alternative to the conditional (ternary) operator (?:), it'll run expression by the conditional result.

  • Max[T constraints.Ordered](a, b T) T

    Gets the maximum value of two values.

  • Min[T constraints.Ordered](a, b T) T

    Gets the minimum value of two values.

Type
  • IsSameType(v1, v2 any) bool

    Compares two values' type.

  • IsSameRawType(v1, v2 any) bool

    Compares two values' type without pointer.

  • TypeOf(v any) string

    Gets the type of the value represented in string.

  • RawTypeOf(v any) string

    Gets the type string name without pointer.

  • GetElem(o any) any

    Gets element without pointer.

Pointer and Value
  • Pointer[T any](v T) *T

    Gets the pointer of a value.

  • func Value[T any](v *T) T

    Gets the value of a pointer, or the zero value of the type if the pointer is nil.

  • ValueWithDefault[T any](v *T, defaultValue T) T

    Gets the value of a pointer, or the default value if the pointer is nil.

  • PointerSlice[T any](v []T) *[]T

    Converts a slice to a pointer slice.

  • ValueSlice[T any](v *[]T) []T

    Converts a pointer slice to a slice.

  • PointerMap[K comparable, V any](v map[K]V) map[K]*V

    Converts a map to a pointer map.

  • ValueMap[K comparable, V any](v map[K]*V) map[K]V

    Converts a pointer map to a map.

License

Distributed under the MIT License. See LICENSE file for more information.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Version = "v0.1.2"

Functions

func Conditional

func Conditional[T any](cond bool, trueExpr, falseExpr T) T

Conditional is an alternative to the conditional (ternary) operator (?:).

func ConditionalExpr added in v0.1.1

func ConditionalExpr[T any](cond bool, trueExpr, falseExpr func() T) T

ConditionalExpr is an alternative to the conditional (ternary) operator (?:), it'll run expression by the conditional result.

func GetElem

func GetElem(o any) any

GetElem returns the element without pointer.

func IsSameRawType

func IsSameRawType(v1, v2 any) bool

IsSameRawType compares two values' type without pointer.

func IsSameType

func IsSameType(v1, v2 any) bool

IsSameType compares two values' type.

func Max

func Max[T constraints.Ordered](v1, v2 T) T

Max returns the maximum value between v1 and v2.

func Min

func Min[T constraints.Ordered](v1, v2 T) T

Min returns the minimum value between v1 and v2.

func Pointer

func Pointer[T any](v T) *T

Pointer returns a pointer to the value passed in.

func PointerMap

func PointerMap[K comparable, V any](src map[K]V) map[K]*V

PointerMap converts a map of values into a map of pointers.

func PointerSlice

func PointerSlice[T any](src []T) []*T

PointerToSlice converts a slice of values into a slice of pointers.

func RawTypeOf

func RawTypeOf(v any) string

RawTypeOf returns the type string name without pointer.

func TypeOf added in v0.1.1

func TypeOf(v any) string

TypeOf returns the type of the value represented in string.

func Value

func Value[T any](v *T) T

Value returns the value of a pointer, or the zero value of the type if the pointer is nil.

func ValueMap

func ValueMap[K comparable, V any](src map[K]*V) map[K]V

ValueMap converts a map of pointers into a map of values.

func ValueSlice

func ValueSlice[T any](src []*T) []T

ValueSlice converts a slice of pointers into a slice of values.

func ValueWithDefault

func ValueWithDefault[T any](val *T, defaultVal T) T

ValueWithDefault returns the value passed in if it is not nil, otherwise returns the default value.

Types

This section is empty.

Jump to

Keyboard shortcuts

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