lazy

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2022 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package lazy allows for lazily-initialized variables via New(). These are often used for globals representing dependencies. The following example shows how to use enve and lazy to configure and initialize a postgres database using github.com/lib/pq.

	import (
    	"gitlab.com/efronlicht/estd/enve"
    	"gitlab.com/efronlicht/estd/lazy"
    	"database/sql"
 	_ "github.com/lib/pq" // register postgres sql driver
	)
		var Postgres = lazy.New[*sql.DB](lazy.Unwrap(func() *sql.DB, error) {
			return sql.Open("postgres", pgConnStrFromEnv())
		})
		func pgConnStrFromEnv() string
			const (
				defaultUser = "efron"
				defaultDBName = "examples"
				defaultSSLMode = "require"
			)
			return fmt.Sprintf("user=%s dbname=%s sslmode=%s",
				enve.StringOr("PG_USER", defaultUser),
				enve.StringOr("PG_DBNAME", defaultDB),
				enve.StringOr("sslmode", defaultSSL),
			)
		}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New[T any](f func() T) *lazy[T]

New creates a lazy-evaluating value of type T. The first (and ONLY the first) call to Get() on the resulting value will call f() and cache and return it's value. Subsequent calls will return the same value.

func Unwrap

func Unwrap[T any](f func() (T, error)) func() T

Unwrap turns a func()(T, error) into a func() T, panicking if the error wasn't nil. Useful for lazily-evaluted dependencies.

Types

This section is empty.

Jump to

Keyboard shortcuts

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