package
1.2.4
Repository: https://github.com/ccheers/xpkg.git
Documentation: pkg.go.dev

# README

group

import "github.com/ccheers/xpkg/generic/containerx/group"

Package group provides a sample lazy load container. The group only creating a new object not until the object is needed by user. And it will cache all the objects to reduce the creation of object.

Index

type Group

Group is a lazy load container.

type Group[T any] struct {
    sync.RWMutex
    // contains filtered or unexported fields
}

func NewGroup

func NewGroup[T any](new func() T) *Group[T]

NewGroup news a group container.

func (*Group[T]) Clear

func (g *Group[T]) Clear()

Clear deletes all objects.

func (*Group[T]) Get

func (g *Group[T]) Get(key string) T

Get gets the object by the given key.

Example

{
	new := func() *Counter {
		fmt.Println("Only Once")
		return &Counter{}
	}
	group := NewGroup(new)

	group.Get("pass").Incr()

	group.Get("pass").Incr()

}

Output

Only Once

func (*Group[T]) Reset

func (g *Group[T]) Reset(new func() T)

Reset resets the new function and deletes all existing objects.

Example

{
	new := func() *Counter {
		return &Counter{}
	}
	group := NewGroup(new)

	newV2 := func() *Counter {
		fmt.Println("New V2")
		return &Counter{}
	}

	group.Reset(newV2)

	group.Get("pass").Incr()

}

Output

New V2

Generated by gomarkdoc

# Functions

NewGroup news a group container.

# Structs

Group is a lazy load container.