typeutil

package
v1.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Package typeutil provides convenience utilities for working with go types

Primarily, consists of useful extensions over collections, primitives and iterators

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ceil added in v1.0.1

func Ceil[T numeric](x float64) T

Ceil returns the least integer value greater than or equal to x, casted to type T.

func ChanSeq

func ChanSeq[T any](seq iter.Seq[T]) <-chan T

ChanSeq accepts iterator sequence and returns the channel representation of it.

Keep in mind that the channel is unbuffered, so can lead to deadlocks if routine is unable to read from it due to external locks.

func ChanSeqSized added in v1.0.4

func ChanSeqSized[T any](seq iter.Seq[T], size int) <-chan T

ChanSeqSized accepts iterator sequence and channel size, and returns the channel representation of iterator.

func ChanSlice

func ChanSlice[T any](vs []T) <-chan T

ChanSlice creates a read-only channel with values from provided slice.

func Chunk

func Chunk[T any](s []T, chunkSize int) [][]T

Chunk returns a slice of chunks of original slice. All chunks except the last one are of size chunkSize.

func CopySeq

func CopySeq[T any](seq iter.Seq[T]) (iter.Seq[T], iter.Seq[T])

func DaysIn

func DaysIn(m time.Month, year int) int

DaysIn returns the day count in month, accounting for a leap year.

func DeduplicateSeq

func DeduplicateSeq[T comparable](seq iter.Seq[T]) iter.Seq[T]

DeduplicateSeq accepts iterator sequence, and returns unique values sequence.

func DivUp added in v1.0.1

func DivUp[T numeric](x, y T) T

DivUp divides x, y (converted to float64 for accuracy first), then applies Ceil on top.

func EndOfDay

func EndOfDay(t time.Time) time.Time

EndOfDay returns the last instance of the same day in current location.

func FirstOfMonth

func FirstOfMonth(t time.Time) time.Time

FirstOfMonth returns the first instant of the month in current location.

func IsWeekday

func IsWeekday(t time.Time) bool

IsWeekday returns if the specified time is within standard week days (time.Monday through time.Friday including).

func LastOfMonth

func LastOfMonth(t time.Time) time.Time

LastOfMonth returns the last instant of month in current location.

func Map

func Map[T any, U any](seq iter.Seq[T], fn func(T) U) iter.Seq[U]

Map accepts iterator sequence and map function, returns sequence of products.

func SafeSlice

func SafeSlice[T any](x []T) []T

SafeSlice returns always non-nil slice of T

Useful to return in DTOs or functions which results are relied upon during marshaling.

func Sorted

func Sorted[K cmp.Ordered, V any](m map[K]V) map[K]V

Sorted returns map with key-value pairs sorted in order of keys.

func Timestamp

func Timestamp(t time.Time) int64

Timestamp converts time.Time to unix timestamp. if time.Time is a zero time, returned value is 0 (zeroish return).

func TruncateString

func TruncateString(s string, limit int) string

TruncateString returns at most limit characters of provided string.

Types

This section is empty.

Jump to

Keyboard shortcuts

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