/goext

Extended functionalities that are missing in the Go standard library but frequently used in other languages.

Primary LanguageGoMIT LicenseMIT

GoExt

Extended functionalities that are missing in the Go standard library but frequently used in other languages.

Especially for JavaScript developers, these packages should make us feel right at home.

Install

go get github.com/ayonli/goext

Functions

goext.ReadAll

func ReadAll[T any](ch <-chan T) []T

ReadAll reads all values from the channel at once.


goext.Ok

func Ok[R any](res R, err error) R

Ok asserts a typical Golang function call which returns a result and an error is successful and returns the result, it panics if the return error is not nil. This function should be composite with the goext.Try() function, allowing the program to bubble the error and catch it from outside.

Example

_, err := goext.Try(func () int {
    res1 := goext.Ok(someCall())
    res2 := goext.Ok(anotherCall())
    return 0
})

goext.Try

func Try[R any](fn func() R) (res R, err error)

Try runs a function in a safe context where if it or what's inside it panics, the panic reason can be caught and returned as a normal error.

Example

_, err := goext.Try(func () int {
    res1 := goext.Ok(someCall())
    res2 := goext.Ok(anotherCall())
    return 0
})

goext.Queue

func Queue[T any](handler func(data T), bufferSize int) IQueue[T]

Queue processes data sequentially by the given handler function and prevents concurrency conflicts, it returns a queue instance that we can push data into.

bufferSize is the maximum capacity of the underlying channel, once reached, the push operation will block until there is new space available. Bu default, this option is not set and use a non-buffered channel instead.


goext.Throttle

func Throttle[A any, R any, Fn func(arg A) (R, error)](
    handler Fn,
    duration time.Duration,
    forKey string,
    noWait bool,
) Fn

Creates a throttled function that will only be run once in a certain amount of time.

If a subsequent call happens within the duration, the previous result will be returned and the handler function will not be invoked.

If forKey is provided, use the throttle strategy for the given key, this will keep the result in a global cache, binding new handler function for the same key will result in the same result as the previous, unless the duration has passed. This mechanism guarantees that both creating the throttled function in function scopes and overwriting the handler are possible.

If noWait is turned on, respond with the last cache (if available) immediately, even if it has expired, and update the cache in the background.

Sub-packages

  • async (Since v0.2.0) Package async provides functions to run functions in other goroutines and wait for their results.
  • mathx Additional functions for math calculation that are missing in the standard library.
  • stringx Additional functions for string processing that are missing in the standard library.
    • mbstring Additional functions for processing strings in multi-byte sequence.
  • slicex Additional functions for playing with slices and reduce mistakes.
  • mapx Additional functions for dealing with maps.
  • structx (Since v0.3.0) Functions used to manipulate structs.
  • number (Since v0.4.0) Functions for dealing with numbers.
  • oop Object-oriented abstract wrappers for basic data structures.
    • String is an object-oriented abstract that works around multi-byte strings.
    • List is an objected-oriented abstract that works around the slice and acts as a dynamic array.
  • collections Object-oriented abstract wrappers for basic types.
    • Set is an object-oriented collection that stores unique items and is thread-safe.
    • Map is an object-oriented collection of map with ordered keys and thread-safe by default.
    • CiMap Thread-safe case-insensitive map, keys are case-insensitive.
    • BiMap Thread-safe bi-directional map, keys and values are unique and map to each other.