Skip to content
/ lazy Public

A lazy value loader, it can be used to load expensive computation only when it is needed and cache the result for future use.

License

Notifications You must be signed in to change notification settings

josestg/lazy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Lazy

A lazy value loader, it can be used to load expensive computation only when it is needed and cache the result for future use.

Installation

go get github.com/josestg/lazy

Example

package main

import (
	"fmt"
	"time"

	"github.com/josestg/lazy"
)

func main() {
	// Create a lazy value loader.
	l := lazy.New(func() (string, error) {
		time.Sleep(1 * time.Second)
		return "Hello, World!", nil
	})

	// Get the value for the first time will be slow.
	fmt.Println(l.Value())  // Hello, World!
	fmt.Println(l.Loaded()) // true

	// Get the value for the next will return the cached value.
	fmt.Println(l.Value()) // Hello, World!
	fmt.Println(l.Value()) // Hello, World!
}

About

A lazy value loader, it can be used to load expensive computation only when it is needed and cache the result for future use.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages