Skip to content

Latest commit

 

History

History
55 lines (41 loc) · 812 Bytes

README.md

File metadata and controls

55 lines (41 loc) · 812 Bytes

Lazy

A generic lazy value loader, with simple API. It offers a simple way to lazy-load expensive values when first needed, and instant access to them later.

Installation

go get github.com/asmsh/lazy

Example

package main

import (
	"bytes"
	"fmt"
	"sync"
	"text/template"

	"github.com/asmsh/lazy"
)

var helloTmpl = lazy.NewValue(func() (*template.Template, error) {
	tmplTxt := `Hello {{.}}!`
	tmpl := template.Must(template.New("hello").Parse(tmplTxt))
	return tmpl, nil
})

func main() {
	users := []string{
		"UserA",
		"UserB",
		"UserC",
	}

	wg := sync.WaitGroup{}
	wg.Add(len(users))

	for _, v := range users {
		user := v

		go func() {
			defer wg.Done()

			b := bytes.Buffer{}
			helloTmpl.Val().Execute(&b, user)
			fmt.Println(b.String())
		}()
	}

	wg.Wait()
}