utls

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2023 License: MIT Imports: 2 Imported by: 0

README

utls

The utls package is a package filled with functions that ought to be in golang, but aren't for reasons unknown. Sometimes these can be found scattered in the exp package but since I often end up re-implementing these anyway, I just wanted to centralize everything in one place.

This package contains the following functions:

  • ToPtr: converts a value to a pointer
  • ToVal: converts a pointer to its value if possible and leaves an ok bool if the pointer is not nil
  • SliceContains: checks if a slice contains a value
  • MapContains: checks if a map contains a key
  • SliceToMap: converts a slice to a map where each key is a value in the slice and each corresponding value is the boolean value true
  • Min: returns the minimum of two ordered values
  • Max: returns the maximum of two ordered values

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MapContains

func MapContains[T comparable, U any](m map[T]U, item T) bool

MapContains takes in a map and an item to check for as a key in that map. If the item is a key in the map, it returns true; otherwise it returns false.

func Max

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

Max returns the maximum of two values.

func Min

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

Min returns the minimum of two values.

func SliceContains

func SliceContains[S ~[]T, T comparable](slice S, item T) bool

SliceContains takes in a slice and an item to check for in the slice. If the item is in the slice, it returns true, otherwise it returns false.

func SliceToMap added in v0.2.0

func SliceToMap[S ~[]T, T comparable](slice S) map[T]bool

SliceToMap takes in a slice and returns a map with the slice values as keys and each corresponding value set to true. If the slice contains duplicate values, the map will only contain one each duplicated value.

func ToPtr

func ToPtr[T any](x T) *T

ToPtr takes any x and returns a pointer to that ptr

func ToVal

func ToVal[T any](ptr *T) (val T, ok bool)

ToVal takes any ptr and returns the value of that pointer if it exists and sets ok to true. If the pointer is nil, it returns the zero value of the type and sets ok to false.

Types

This section is empty.

Jump to

Keyboard shortcuts

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