group

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

type Factory[T any] func() T

Factory is a function that creates an object of type T.

type Group

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

Group is a lazy load container.

func NewGroup

func NewGroup[T any](factory Factory[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
group := NewGroup(func() any {
	fmt.Println("Only Once")
	return &Counter{}
})

// Create a new Counter
group.Get("pass").(*Counter).Incr()

// Get the created Counter again.
group.Get("pass").(*Counter).Incr()
Output:
Only Once

func (*Group[T]) Reset

func (g *Group[T]) Reset(factory Factory[T])

Reset resets the new function and deletes all existing objects.

Example
group := NewGroup(func() any {
	return &Counter{}
})

// Reset the new function and clear all created objects.
group.Reset(func() any {
	fmt.Println("reset")
	return &Counter{}
})

// Create a new Counter
group.Get("pass").(*Counter).Incr()
Output:
reset

Jump to

Keyboard shortcuts

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