lazy

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2022 License: MIT Imports: 1 Imported by: 0

README

Lazy

GitHub go.mod Go version of a Go module GoDoc reference example Go Report Card CircleCI

Simple generic concurrent-safe lazy initialization container for Go.

Installation

go get -u github.com/mono83/lazy

Usage

import "github.com/mono83/lazy"

// Static value supplier
greet := lazy.Const("Hello, world")
fmt.Println(greet())

// With supplier
some := lazy.New(func() any { /* Some heavy operations be execute once */ })
value := some()

// Also there are constructors for value + error tuples
lazy.ConstE[T](42)                // -> func() (T,error)
lazy.Error[T](nil)                // -> func() (T,error)
lazy.NewE(func(T any, err error)) // -> func() (T,error)
Benchmarks
$ go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: github.com/mono83/lazy
cpu: Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz
BenchmarkConst-12       61397071                20.70 ns/op           24 B/op          1 allocs/op
BenchmarkNew-12         18599815                66.76 ns/op           72 B/op          3 allocs/op
BenchmarkAll/Read-const-1times-12               522478002                2.310 ns/op           0 B/op          0 allocs/op
BenchmarkAll/Read-new-1times-12                 422763762                2.863 ns/op           0 B/op          0 allocs/op
BenchmarkAll/Read-const-100times-12              7491662               162.9 ns/op             0 B/op          0 allocs/op
BenchmarkAll/Read-new-100times-12                4521046               260.5 ns/op             0 B/op          0 allocs/op
BenchmarkAll/Read-const-1000times-12              748990              1563 ns/op               0 B/op          0 allocs/op
BenchmarkAll/Read-new-1000times-12                476796              2619 ns/op               0 B/op          0 allocs/op
BenchmarkAll/Read-const-10000times-12              78136             15567 ns/op               0 B/op          0 allocs/op
BenchmarkAll/Read-new-10000times-12                46693             26274 ns/op               0 B/op          0 allocs/op
PASS
ok      github.com/mono83/lazy  14.628s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Const

func Const[T any](value T) func() T

Const returns lazy value supplier with constant value.

func ConstE

func ConstE[T any](value T) func() (T, error)

ConstE returns lazy value supplier with constant value and no error.

func Error

func Error[T any](err error) func() (T, error)

Error returns lazy value supplier with constant error and no value.

func New

func New[T any](supply func() T) func() T

New constructs lazy value supplier built over given supplier function invoked only once.

func NewE

func NewE[T any](supply func() (T, error)) func() (T, error)

NewE constructs lazy value supplier built over given supplier function invoked only once.

Types

This section is empty.

Jump to

Keyboard shortcuts

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