gohelp

package module
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2025 License: BSD-2-Clause Imports: 13 Imported by: 11

README

gohelp

helper functions

Documentation

Index

Constants

View Source
const (
	AnsiReset = "\x1b[0m"

	AnsiBlack   = "\x1b[30;1m"
	AnsiRed     = "\x1b[31;1m"
	AnsiGreen   = "\x1b[32;1m"
	AnsiYellow  = "\x1b[33;1m"
	AnsiBlue    = "\x1b[34;1m"
	AnsiMagenta = "\x1b[35;1m"
	AnsiCyan    = "\x1b[36;1m"
	AnsiWhite   = "\x1b[37;1m"

	AnsiCustom     = "\x1b[38;5;%vm"
	AnsiCustomCode = "\x1b[38;5;"

	AnsiBackgroundBlack   = "\x1b[40;1m"
	AnsiBackgroundRed     = "\x1b[42;1m"
	AnsiBackgroundGreen   = "\x1b[42;1m"
	AnsiBackgroundYellow  = "\x1b[43;1m"
	AnsiBackgroundBlue    = "\x1b[44;1m"
	AnsiBackgroundMagenta = "\x1b[45;1m"
	AnsiBackgroundCyan    = "\x1b[46;1m"
	AnsiBackgroundWhite   = "\x1b[47;1m"

	AnsiBackgroundCustom = "\x1b[48;5;%vm"

	AnsiBold      = "\x1b[1m"
	AnsiUnderline = "\x1b[4m"
	AnsiReversed  = "\x1b[7m"

	AnsiCursorUp    = "\x1b[%vA"
	AnsiCursorDown  = "\x1b[%vB"
	AnsiCursorRight = "\x1b[%vC"
	AnsiCursorLeft  = "\x1b[%vD"
)
View Source
const Charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

Variables

View Source
var (
	// AnsiRainbowCodes color code for rainbow words
	// 1 - red
	// 208 - orange
	// 11 - yellow
	// 40 - green
	// 45 - blue
	// 27 - blue
	// 93 - purple
	AnsiRainbowCodes = []string{"1", "208", "11", "40", "45", "27", "93"}
)

Functions

func AppendUnique added in v1.0.0

func AppendUnique[T comparable](slice []T, values ...T) []T

AppendUnique append unique value to array

func ArrayIntersect added in v1.0.0

func ArrayIntersect[T comparable](target []T, destination []T) (intersect []T, removedTargets int32)

ArrayIntersect shows duplicated elements in both slices

func BeforeString

func BeforeString(source string, substr string) string

BeforeString get string in source before substring

func Blue added in v1.0.0

func Blue(v interface{}) string

Blue wrap into blue color

func CheckBracers added in v1.1.2

func CheckBracers(source string, stack Stack[byte]) error

CheckBracers check if bracers are correct

func CheckTypeOf added in v1.6.2

func CheckTypeOf(value []byte) (isUint, isInt, isFloat, isBool, isString bool)

CheckTypeOf check value for type 00001111 - 1 - uint - 2 - int - 4 - float - 8 - string

func Convert added in v1.1.1

func Convert[S, T any](source S) T

Convert unsafe convert. Use carefully

func Cyan added in v1.0.0

func Cyan(v interface{}) string

Cyan wrap into cyan color

func Each added in v1.0.0

func Each[S any](s []S, callback func(S))

Each iterate thought []S and apply on each item collback

func ExistsInArray added in v1.0.0

func ExistsInArray[T comparable](value T, slice []T) bool

ExistsInArray Check if item exists in slice

func Filter added in v1.0.0

func Filter[S any](s []S, callback func(S) bool) []S

Filter argument source via callback

func GetRndDateTime

func GetRndDateTime() time.Time

func GetRndId

func GetRndId() int

func GetRndNumber

func GetRndNumber(min int, max int) int

func GetRndPhone

func GetRndPhone() int

func Green added in v1.0.0

func Green(v interface{}) string

Green wrap into green color

func Implode

func Implode[T comparable](values []T, sep string) string

Implode join values via separator

func Index added in v1.0.0

func Index[T comparable](value T, slice []T) int

Index return index of item if exists. -1 if not

func Magenta added in v1.0.0

func Magenta(v interface{}) string

Magenta wrap into magenta color

func Map added in v1.0.0

func Map[S, D any](s []S, callback func(S) D) []D

Map argument source and result destination

func NewUUID

func NewUUID() string

NewUUID uuid constructor

func Ptr added in v1.1.1

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

Ptr get pointer of value

func Rainbow added in v1.0.0

func Rainbow(str string, sep rune) string

Rainbow crete rainbow string str - string for transformation sep - string separator

func RandString

func RandString(length int) string

RandString create random string

func Red added in v1.0.0

func Red(v interface{}) string

Red wrap into red color

func Reduce added in v1.0.0

func Reduce[S, D any](init D, s []S, callback func(D, S) D) D

Reduce implementation

func ToCamelCase

func ToCamelCase(str string, isFirstTitle bool) string

ToCamelCase transform to camelCase

func ToUnderscore

func ToUnderscore(str string) string

ToUnderscore transform string to underscore case

func Underline added in v1.0.0

func Underline(v interface{}) string

Underline wrap into underline text

func Unique added in v1.0.0

func Unique[T comparable](left []T, right []T) []T

Unique create unique slice between left and right slices

func UniqueCompare added in v1.7.1

func UniqueCompare[S comparable](left []S, right []S) (common []S, removed []S, added []S)

UniqueCompare Unique compare unique values between arrays

func UniqueLeft added in v1.0.0

func UniqueLeft[T comparable](left []T, right []T) []T

UniqueLeft create unique slice from left and right slices

func Yellow added in v1.0.0

func Yellow(v interface{}) string

Yellow wrap into yellow color

Types

type EntityCache added in v1.4.0

type EntityCache[E comparable, T any] struct {
	// contains filtered or unexported fields
}

EntityCache entity data cache

func NewEntityCache added in v1.4.0

func NewEntityCache[E comparable, T any](refreshPeriod, maxFailedCount uint16, callback RefreshEntityCallback[E, T], id ...E) *EntityCache[E, T]

NewEntityCache create new entity cache E - identifier type T - type of entity cache refreshPeriod - how often will be refresh executed callback - refresh function

func (*EntityCache[E, T]) AddItemIds added in v1.4.0

func (c *EntityCache[E, T]) AddItemIds(id ...E)

AddItemIds add entity ids for refresh

func (*EntityCache[E, T]) Filter added in v1.7.0

func (c *EntityCache[E, T]) Filter(searchable []E, compare func(item *T) bool) (result []*T)

Filter items thread safe searchable - list of ids to iteration. if len(searchable) == 0 then iteration over entityMap compare - is item satisfies for search condition result - list of resulted items

func (*EntityCache[E, T]) GetAll added in v1.4.0

func (c *EntityCache[E, T]) GetAll() map[E]*T

GetAll get all items

func (*EntityCache[E, T]) GetEntity added in v1.4.0

func (c *EntityCache[E, T]) GetEntity(id E) *T

GetEntity get single entity

func (*EntityCache[E, T]) GetItemIds added in v1.4.0

func (c *EntityCache[E, T]) GetItemIds() []E

GetItemIds get refresh entity ids

func (*EntityCache[E, T]) Idle added in v1.4.0

func (c *EntityCache[E, T]) Idle(ctx context.Context) error

Idle wait for next time refresh event

func (*EntityCache[E, T]) Len added in v1.4.1

func (c *EntityCache[E, T]) Len() int

Len return len of entity map

func (*EntityCache[E, T]) Map added in v1.6.0

func (c *EntityCache[E, T]) Map(next func(id E, item *T), ordered bool)

Map iterator through cache items next - single item callback iterator ordered - means that ids define order for item iterations

func (*EntityCache[E, T]) Refresh added in v1.4.0

func (c *EntityCache[E, T]) Refresh() error

Refresh cache function

func (*EntityCache[E, T]) RefreshUntilSuccess added in v1.6.1

func (c *EntityCache[E, T]) RefreshUntilSuccess() error

RefreshUntilSuccess refresh until c.maxFailedCount

func (*EntityCache[E, T]) Set added in v1.4.0

func (c *EntityCache[E, T]) Set(entityMap map[E]*T)

Set entity map

func (*EntityCache[E, T]) SetCallback added in v1.4.0

func (c *EntityCache[E, T]) SetCallback(callback RefreshEntityCallback[E, T])

SetCallback set refresh callback in case when nil cache will be not refreshed

func (*EntityCache[E, T]) SetEntity added in v1.4.1

func (c *EntityCache[E, T]) SetEntity(id E, item *T)

SetEntity set single entity

func (*EntityCache[E, T]) SetItemIds added in v1.4.0

func (c *EntityCache[E, T]) SetItemIds(id ...E)

SetItemIds set entity ids for refresh

func (*EntityCache[E, T]) SortOrder added in v1.4.0

func (c *EntityCache[E, T]) SortOrder(s func(a, b *T) bool) []E

SortOrder get items sort order according to sort func

func (*EntityCache[E, T]) UnSetEntity added in v1.5.1

func (c *EntityCache[E, T]) UnSetEntity(id E)

UnSetEntity unset single entity

func (*EntityCache[E, T]) UnSetItemIds added in v1.5.1

func (c *EntityCache[E, T]) UnSetItemIds(ids ...E)

UnSetItemIds unset item ids

type Iterator added in v1.0.0

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator struct

func NewIterator added in v1.0.0

func NewIterator(len int) *Iterator

NewIterator Iterator constructor

func (*Iterator) Count added in v1.0.0

func (c *Iterator) Count() int

Count Iterator Get count of items

func (*Iterator) Cursor added in v1.0.0

func (c *Iterator) Cursor() int

Cursor Iterator Get cursor

func (*Iterator) Next added in v1.0.0

func (c *Iterator) Next() bool

Next Iterator next iteration

func (*Iterator) Reset added in v1.0.0

func (c *Iterator) Reset() *Iterator

Reset cursor

func (*Iterator) SetCount added in v1.0.0

func (c *Iterator) SetCount(count int) *Iterator

SetCount Iterator Set count

type Pipe added in v1.5.0

type Pipe struct {
	// Instruction for call
	Instruction PipeInstruction
	// argument for call
	Arguments []PipeArguments
}

Pipe single call

func NewPipe added in v1.5.0

func NewPipe(lenI, lenA int) Pipe

NewPipe Init pipe

func (*Pipe) Push added in v1.5.0

func (d *Pipe) Push(instruction PipeInstruction, args ...any)

Push Instruction to pipeline with arguments

func (*Pipe) Reset added in v1.5.0

func (d *Pipe) Reset()

Reset reset pipe

type PipeArguments added in v1.5.0

type PipeArguments []any

PipeArguments list of Arguments

type PipeInstruction added in v1.5.0

type PipeInstruction []byte

PipeInstruction Instruction declaration

type PipeLine added in v1.5.0

type PipeLine struct {
	// contains filtered or unexported fields
}

PipeLine collected pipe items

func NewPipeLine added in v1.5.0

func NewPipeLine(lenI, lenA int) *PipeLine

NewPipeLine Create new pipeline with arguments lenI len of instruction field lenA len of arguments list

func (*PipeLine) Add added in v1.5.0

func (s *PipeLine) Add(instruction PipeInstruction, args ...any)

Add save new call

func (*PipeLine) Current added in v1.5.0

func (s *PipeLine) Current(instruction PipeInstruction, args ...any)

Current save into current call

func (*PipeLine) Deserialize added in v1.5.0

func (s *PipeLine) Deserialize(buf *bytes.Buffer) error

Deserialize pipeline

func (*PipeLine) Items added in v1.5.0

func (s *PipeLine) Items() []Pipe

Items item iterator

func (*PipeLine) Reset added in v1.5.0

func (s *PipeLine) Reset()

Reset reset

func (*PipeLine) Serialize added in v1.5.0

func (s *PipeLine) Serialize(buf *bytes.Buffer) ([]byte, error)

Serialize pipeline

type RefreshEntityCallback added in v1.4.0

type RefreshEntityCallback[E comparable, T any] func(id ...E) (map[E]*T, error)

RefreshEntityCallback refresh entity callback

type Stack added in v1.0.0

type Stack[T any] []T

Stack of any type

func (Stack[T]) Pop added in v1.0.0

func (s Stack[T]) Pop() (T, bool, Stack[T])

Pop value

func (Stack[T]) Push added in v1.0.0

func (s Stack[T]) Push(v T) Stack[T]

Push value

type Tree added in v1.3.0

type Tree[T any] struct {
	// Identifier
	Id int `json:"id"`
	// Parent item
	Parent *Tree[T] `json:"-"`
	// Specific tree data
	Data *T `json:"data"`
	// Children
	Children TreeList[T] `json:"children"`
}

Tree simple tree

func (*Tree[T]) FirstChild added in v1.3.0

func (t *Tree[T]) FirstChild() *Tree[T]

FirstChild get first child

func (*Tree[T]) GetById added in v1.3.0

func (t *Tree[T]) GetById(id int) *Tree[T]

GetById get by identifier

func (*Tree[T]) LastChild added in v1.3.0

func (t *Tree[T]) LastChild() *Tree[T]

LastChild get last child

func (*Tree[T]) Lookup added in v1.3.0

func (t *Tree[T]) Lookup(found func(i *Tree[T]) bool) TreeList[T]

Lookup function

func (*Tree[T]) Map added in v1.3.0

func (t *Tree[T]) Map(c func(i *Tree[T]))

Map tree items

func (*Tree[T]) Path added in v1.3.0

func (t *Tree[T]) Path(delimiter string, c func(i *Tree[T]) string) string

Path build path

func (*Tree[T]) UnmarshalJSON added in v1.3.0

func (t *Tree[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON unmarshal data with parents

type TreeList added in v1.3.0

type TreeList[T any] []*Tree[T]

TreeList list of tree items

func (TreeList[T]) First added in v1.3.0

func (l TreeList[T]) First() *Tree[T]

First tree item

func (TreeList[T]) GetById added in v1.3.0

func (l TreeList[T]) GetById(id int) *Tree[T]

GetById find by id

func (TreeList[T]) Last added in v1.3.0

func (l TreeList[T]) Last() *Tree[T]

Last tree item

func (TreeList[T]) Lookup added in v1.3.0

func (l TreeList[T]) Lookup(found func(i *Tree[T]) bool) TreeList[T]

Lookup function

type UUID

type UUID struct {
	// contains filtered or unexported fields
}

UUID type

func ParseUUID added in v0.3.2

func ParseUUID(data string) *UUID

ParseUUID uuid parser

func (*UUID) Generate

func (u *UUID) Generate() *UUID

Generate uuid

func (*UUID) Get

func (u *UUID) Get() *string

Get uuid

func (*UUID) IsValid

func (u *UUID) IsValid() bool

IsValid is uuid valid

func (*UUID) Reset

func (u *UUID) Reset() *UUID

Reset uuid

Jump to

Keyboard shortcuts

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