gopkg

package module
v1.0.26 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2025 License: MIT Imports: 9 Imported by: 0

README

Golang Pkg

Install Dependencies

go get github.com/nhnghia272/gopkg

Example

package main

import (
	"fmt"
	"time"

	"github.com/nhnghia272/gopkg"
)

func main() {
	// Initialize a new Async
	as := gopkg.Async()
	cache := gopkg.NewCacheShard[string](gopkg.CacheConfig{Shard: 1})

	// Add goroutine
	as.Go(func() {
		password := gopkg.Random(20)
		fmt.Println("RandomPassword:", password)

		cache.Set("test", password, time.Second)
		fmt.Println(cache.Get("test"))
	})

	arr := []int{1, 2, 3, 4, 5, 2}
	fmt.Println("Init Slice:", arr)

	gopkg.LoopFunc(arr, func(e int) { fmt.Println("LoopFunc:", e) })

	gopkg.LoopParallelFunc(arr, func(e int) { fmt.Println("LoopParallelFunc:", e) })

	gopkg.LoopAsyncFunc(arr, func(e int) { fmt.Println("LoopAsyncFunc with wait:", e) }, true)

	gopkg.LoopAsyncFunc(arr, func(e int) { fmt.Println("LoopAsyncFunc without wait:", e) }, false)

	gopkg.LoopWithIndexFunc(arr, func(e int, i int) { fmt.Println("LoopWithIndexFunc:", e, i) })

	gopkg.LoopWithIndexParallelFunc(arr, func(e int, i int) { fmt.Println("LoopWithIndexParallelFunc:", e, i) })

	gopkg.LoopWithIndexAsyncFunc(arr, func(e int, i int) { fmt.Println("LoopWithIndexAsyncFunc with wait:", e, i) }, true)

	gopkg.LoopWithIndexAsyncFunc(arr, func(e int, i int) { fmt.Println("LoopWithIndexAsyncFunc without wait:", e, i) }, false)

	fmt.Println("UniqueFunc:", gopkg.UniqueFunc(arr, func(e int) int { return e }))

	fmt.Println("GroupFunc:", gopkg.GroupFunc(arr, func(e int) int { return e }))

	fmt.Println("SliceToMapFunc:", gopkg.SliceToMapFunc(arr, func(e int) int { return e }))

	fmt.Println("MapFunc:", gopkg.MapFunc(arr, func(e int) int { return e * 2 }))

	mp, err := gopkg.MapParallelFunc(arr, func(e int) int { return e * 3 })
	fmt.Println("MapParallelFunc:", mp, err)

	fmt.Println("FilterFunc:", gopkg.FilterFunc(arr, func(e int) bool { return e%2 == 0 }))

	fp, err := gopkg.FilterParallelFunc(arr, func(e int) bool { return e%2 == 1 })
	fmt.Println("FilterParallelFunc:", fp, err)

	item, found := gopkg.FindFunc(arr, func(e int) bool { return e == 3 })
	fmt.Println("FindFunc:", item, found)

	fmt.Println("ReduceFunc:", gopkg.ReduceFunc(arr, 0, func(a int, e int) int { return a + e }))

	// Wait goroutine finish
	if err := as.Wait(); err != nil {
		fmt.Println(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Async

func Async() *async

func Convert added in v1.0.11

func Convert[E1, E2 any](src E1, des E2) error

func FilterFunc

func FilterFunc[E any](s []E, f func(e E) bool) []E

func FilterParallelFunc added in v1.0.21

func FilterParallelFunc[E any](s []E, f func(e E) bool) ([]E, error)

func FindFunc added in v1.0.14

func FindFunc[E any](s []E, f func(e E) bool) (E, bool)

func GroupFunc added in v1.0.21

func GroupFunc[E any, K comparable](s []E, f func(e E) K) map[K][]E

func LoopAsyncFunc added in v1.0.21

func LoopAsyncFunc[E any](s []E, f func(e E), wait bool) error

func LoopFunc added in v1.0.21

func LoopFunc[E any](s []E, f func(e E))

func LoopParallelFunc added in v1.0.23

func LoopParallelFunc[E any](s []E, f func(e E)) error

func LoopWithIndexAsyncFunc added in v1.0.26

func LoopWithIndexAsyncFunc[E any](s []E, f func(e E, i int), wait bool) error

func LoopWithIndexFunc added in v1.0.26

func LoopWithIndexFunc[E any](s []E, f func(e E, i int))

func LoopWithIndexParallelFunc added in v1.0.26

func LoopWithIndexParallelFunc[E any](s []E, f func(e E, i int)) error

func MapFunc

func MapFunc[E1, E2 any](s []E1, f func(e E1) E2) []E2

func MapParallelFunc added in v1.0.21

func MapParallelFunc[E1, E2 any](s []E1, f func(e E1) E2) ([]E2, error)

func Pointer added in v1.0.11

func Pointer[E any](v E) *E

func PointerMap added in v1.0.11

func PointerMap[E any](vs map[string]E) map[string]*E

func PointerSlice added in v1.0.11

func PointerSlice[E any](vs []E) []*E

func Random

func Random(length uint) string

func RandomNumber

func RandomNumber(length uint) string

func RandomSpecial

func RandomSpecial(length uint) string

func RandomString

func RandomString(length uint) string

func ReduceFunc

func ReduceFunc[E1, E2 any](s []E1, a E2, f func(a E2, e E1) E2) E2

func SliceToMapFunc added in v1.0.21

func SliceToMapFunc[E any, K comparable](s []E, f func(e E) K) map[K]E

func UniqueFunc

func UniqueFunc[E any, K comparable](s []E, f func(e E) K) []E

func Value added in v1.0.11

func Value[E any](p *E) E

func ValueMap added in v1.0.11

func ValueMap[E any](ps map[string]*E) map[string]E

func ValueSlice added in v1.0.11

func ValueSlice[E any](ps []*E) []E

Types

type CacheConfig added in v1.0.10

type CacheConfig struct {
	Shard int
	Clean time.Duration
}

type CacheShard added in v1.0.4

type CacheShard[E any] []*cache[E]

func NewCacheShard added in v1.0.4

func NewCacheShard[E any](config ...CacheConfig) CacheShard[E]

func (CacheShard[E]) Delete added in v1.0.10

func (s CacheShard[E]) Delete(key string) error

func (CacheShard[E]) Get added in v1.0.4

func (s CacheShard[E]) Get(key string) (E, error)

func (CacheShard[E]) Keys added in v1.0.13

func (s CacheShard[E]) Keys() []string

func (CacheShard[E]) Reset added in v1.0.10

func (s CacheShard[E]) Reset() error

func (CacheShard[E]) Set added in v1.0.4

func (s CacheShard[E]) Set(key string, val E, exp time.Duration) error

func (CacheShard[E]) Values added in v1.0.13

func (s CacheShard[E]) Values() []E

Jump to

Keyboard shortcuts

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