group

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2023 License: MIT Imports: 1 Imported by: 0

README

group

group libraries.

Example of use

    import "github.com/liuzw3018/sponge/pkg/container/group"

    type foo struct {
        bar string
    }
    
    gr := group.NewGroup(func () interface{} {
        return &foo{"hello"}
    })

	fmt.Println(gr.Get(*foo).bar)

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 Group

type Group struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

Group is a lazy load container.

func NewGroup

func NewGroup(new func() interface{}) *Group

NewGroup news a group container.

func (*Group) Clear

func (g *Group) Clear()

Clear deletes all objects.

func (*Group) Get

func (g *Group) Get(key string) interface{}

Get gets the object by the given key.

Example
group := NewGroup(func() interface{} {
	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) Reset

func (g *Group) Reset(new func() interface{})

Reset resets the new function and deletes all existing objects.

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

// Reset the new function and clear all created objects.
group.Reset(func() interface{} {
	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