once

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: MIT Imports: 1 Imported by: 0

README

Once

A simple library to do something exactly once.

GitHub Releases Build Status codecov Go Report Card GoDevDoc Donate

Prerequisites

  • Go >= 1.21

Install

go get go.nhat.io/once

Types

Type Description
Once An alias of sync.Once.
Func A function that can be executed exactly once.
Value A function that returns value can be executed exactly once.
Values A function that returns values can be executed exactly once.
FuncMap A map of functions that can be executed exactly once.
ValueMap A map of functions that return value can be executed exactly once.
ValuesMap A map of functions that return values can be executed exactly once.
LazyValueMap A map contains values that are initialized on the first access.

Examples

package once_test

import (
    "fmt"

    "go.nhat.io/once"
)

func ExampleLazyValueMap() {
    type Person struct {
        ID   string
        Name string
    }

    people := once.LazyValueMap[string, *Person]{
        New: func(key string) *Person {
            return &Person{ID: key}
        },
    }

    instance1 := people.Get("1")
    instance2 := people.Get("1")

    fmt.Println(instance2.Name)

    instance1.Name = "John Doe"

    fmt.Println(instance2.Name)

    // Output:
    //
    // John Doe
}

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Paypal donation

paypal

       or scan this

Documentation

Overview

Package once provides a simple way to do something exactly once.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Func

func Func(f func()) func()

Func returns a function that invokes f only once. The returned function may be called concurrently.

If f panics, the returned function will panic with the same value on every call.

func Value

func Value[T any](f func() T) func() T

Value returns a function that invokes f only once and returns the value returned by f. The returned function may be called concurrently.

If f panics, the returned function will panic with the same value on every call.

func Values

func Values[T1, T2 any](f func() (T1, T2)) func() (T1, T2)

Values returns a function that invokes f only once and returns the values returned by f. The returned function may be called concurrently.

If f panics, the returned function will panic with the same value on every call.

Types

type FuncMap

type FuncMap[K any] Map[K, func()]

FuncMap is a map of functions that are only computed once.

func (*FuncMap[K]) Delete

func (m *FuncMap[K]) Delete(key K)

Delete deletes the key and its function from the map.

func (*FuncMap[K]) Do

func (m *FuncMap[K]) Do(key K, f func())

Do executes the function only once for the given key.

If f panics, the function will panic with the same value on every call.

func (*FuncMap[K]) Len

func (m *FuncMap[K]) Len() int

Len returns the number of functions in the map.

type LazyValueMap

type LazyValueMap[K, V any] struct {
	New func(key K) V
	// contains filtered or unexported fields
}

LazyValueMap initializes values when they are first accessed.

Example
package main

import (
	"fmt"

	"go.nhat.io/once"
)

func main() {
	type Person struct {
		ID   string
		Name string
	}

	people := once.LazyValueMap[string, *Person]{
		New: func(key string) *Person {
			return &Person{ID: key}
		},
	}

	instance1 := people.Get("1")
	instance2 := people.Get("1")

	fmt.Println(instance2.Name)

	instance1.Name = "John Doe"

	fmt.Println(instance2.Name)

}
Output:


John Doe

func (*LazyValueMap[K, V]) Delete

func (p *LazyValueMap[K, V]) Delete(key K)

Delete removes the key and its value from the map.

func (*LazyValueMap[K, V]) Get

func (p *LazyValueMap[K, V]) Get(key K) V

Get returns the value for key if it exists. Otherwise, it calls New and stores.

func (*LazyValueMap[K, V]) Len

func (p *LazyValueMap[K, V]) Len() int

Len returns the number of entries in the map.

type Map

type Map[K, V any] sync.Map

Map is simpler version of sync.Map.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K)

Delete removes the key and its value from the map.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K, defaultValue V) V

Get returns the value for key if it exists. Otherwise, it returns default value.

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Len returns the number of entries in the map.

func (*Map[K, V]) Range added in v0.2.0

func (m *Map[K, V]) Range(f func(key K, value V) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

type Once

type Once = sync.Once

Once is an object that will perform exactly one action.

A Once must not be copied after first use.

In the terminology of the Go memory model, the return from f “synchronizes before” the return from any call of once.Do(f).

type ValueMap

type ValueMap[K, V any] Map[K, func() V]

ValueMap is a map of functions that are only computed once.

func (*ValueMap[K, V]) Delete

func (m *ValueMap[K, V]) Delete(key K)

Delete removes the key and its function from the map.

func (*ValueMap[K, V]) Do

func (m *ValueMap[K, V]) Do(key K, f func() V) V

Do executes the function only once for the given key and returns the value returned by f.

If f panics, the function will panic with the same value on every call.

func (*ValueMap[K, V]) Len

func (m *ValueMap[K, V]) Len() int

Len returns the number of functions in the map.

func (*ValueMap[K, V]) Values added in v0.2.0

func (m *ValueMap[K, V]) Values() []V

Values returns all values in the map. If f panics, the function will panic with the same value on every call.

type ValuesMap

type ValuesMap[K, V1, V2 any] Map[K, func() (V1, V2)]

ValuesMap is a map of functions that are only computed once.

func (*ValuesMap[K, V1, V2]) Delete

func (m *ValuesMap[K, V1, V2]) Delete(key K)

Delete removes the key and its function from the map.

func (*ValuesMap[K, V1, V2]) Do

func (m *ValuesMap[K, V1, V2]) Do(key K, f func() (V1, V2)) (V1, V2)

Do executes the function only once for the given key and returns the values returned by f.

If f panics, the function will panic with the same value on every call.

func (*ValuesMap[K, V1, V2]) Len

func (m *ValuesMap[K, V1, V2]) Len() int

Len returns the number of functions in the map.

Jump to

Keyboard shortcuts

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