Skip to content

A simple library to do something exactly once

License

Notifications You must be signed in to change notification settings

nhatthm/go-once

Repository files navigation

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