base

package module
v0.0.0-...-2f8872a Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 7 Imported by: 0

README

Go-Base

Go common utility library.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cache

func Cache[K comparable, V any](buffer Buffer[K, V], key func(...any) K, function func(...any) V, arguments ...any) V

Cache caches the function result into the buffer.

func Fallback

func Fallback[T any](value T, err error) func(T) T

Fallback returns the fallback value if error not nil.

Usage:

x := base.Fallback(time.ParseDuration("invalid_layout"))(10 * time.Second)

fmt.Printf("%v\n", x)

// Prints "10s".

func GetEnvBool

func GetEnvBool(key string) (bool, error)

GetEnvBool gets the bool from environment.

func GetEnvFloat64

func GetEnvFloat64(key string) (float64, error)

GetEnvFloat64 gets the float64 from environment.

func GetEnvInt

func GetEnvInt(key string) (int, error)

GetEnvInt gets the integer from environment.

func GetEnvString

func GetEnvString(key string) (string, error)

GetEnvString gets the string from environment.

func Ignore

func Ignore[T any](value T, _ error) T

Ignore ignores the error.

Usage:

x := base.Ignore(time.ParseDuration("test"))

func Must

func Must[T any](value T, err error) T

Must panics if error not nil.

Usage:

x := base.Must(time.ParseDuration("10s"))

func Ref

func Ref[T any](value T) *T

Ref makes pointer to the value.

Usage:

var x *int = base.Ref(5)

Types

type Buffer

type Buffer[K comparable, V any] interface {

	// Set sets the object into the buffer.
	Set(key K, value V)

	// Get gets the object from buffer.
	Get(key K) V

	// Delete removes the object from the buffer.
	Delete(key K)

	// Keys returns the buffer keys.
	Keys() []K
}

Buffer is a buffer interface.

type DefaultBuffer

type DefaultBuffer[K comparable, V any] struct {
	// contains filtered or unexported fields
}

DefaultBuffer is a default buffer implementation.

func NewDefaultBuffer

func NewDefaultBuffer[K comparable, V any]() *DefaultBuffer[K, V]

NewDefaultBuffer creates the buffer.

func NewDefaultBufferFrom

func NewDefaultBufferFrom[K comparable, V any](data map[K]V) *DefaultBuffer[K, V]

NewDefaultBufferFrom creates the buffer with data.

func (*DefaultBuffer[K, V]) Delete

func (buffer *DefaultBuffer[K, V]) Delete(key K)

Delete remove the object from the buffer.

func (*DefaultBuffer[K, V]) Get

func (buffer *DefaultBuffer[K, V]) Get(key K) V

Get gets the object from the buffer.

func (*DefaultBuffer[K, V]) Keys

func (buffer *DefaultBuffer[K, V]) Keys() []K

Map returns the buffer data.

func (*DefaultBuffer[K, V]) Set

func (buffer *DefaultBuffer[K, V]) Set(key K, value V)

Set sets the object into the buffer.

type TemporalBuffer

type TemporalBuffer[K comparable, V any] struct {
	// contains filtered or unexported fields
}

TemporalBuffer is a temporal buffer implementation.

func NewTemporalBuffer

func NewTemporalBuffer[K comparable, V any](duration time.Duration) *TemporalBuffer[K, V]

NewTemporalBuffer creates the buffer.

func NewTemporalBufferFrom

func NewTemporalBufferFrom[K comparable, V any](data map[K]V, duration time.Duration) *TemporalBuffer[K, V]

NewTemporalBufferFrom creates the buffer with data.

func (*TemporalBuffer[K, V]) Delete

func (buffer *TemporalBuffer[K, V]) Delete(key K)

Delete remove the object from the buffer.

func (*TemporalBuffer[K, V]) Get

func (buffer *TemporalBuffer[K, V]) Get(key K) V

func (*TemporalBuffer[K, V]) Keys

func (buffer *TemporalBuffer[K, V]) Keys() []K

Keys returns the buffer keys.

func (*TemporalBuffer[K, V]) Set

func (buffer *TemporalBuffer[K, V]) Set(key K, value V)

Set sets the object into the buffer.

Jump to

Keyboard shortcuts

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