zerokit

package
v0.147.2 Latest Latest
Warning

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

Go to latest
Published: May 4, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

package zeroutil

The zeroutil package helps with zero value related use-cases such as initialisation.

package mypkg

import (
	"github.com/adamluzsi/frameless/pkg/zerokit"
)

type MyType struct {
	V string
}

func (mt *MyType) getV() string {
	return zeroutil.Init(&mt.V, func() string {
		return "default-value"
	})
}

Documentation

Overview

Package zerokit helps with zero value related use-cases such as initialisation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Coalesce

func Coalesce[T any](vs ...T) T

Coalesce will return the first non-zero value from the provided values.

Example
package main

import (
	"github.com/adamluzsi/frameless/pkg/zerokit"
)

func main() {
	_ = zerokit.Coalesce("", "", "42") // -> "42"
}
Output:

func Init

func Init[T any, I initialiser[T]](v *T, init I) T

Init will initialise a zero value through its pointer (*T), If it's not set, it assigns a value to it based on the supplied initialiser. Init is safe to use concurrently, it has no race condition.

Example
package main

import (
	"github.com/adamluzsi/frameless/pkg/pointer"
	"github.com/adamluzsi/frameless/pkg/zerokit"
)

func main() {
	type MyType struct {
		V *string
	}
	var mt MyType

	_ = zerokit.Init(&mt.V, func() *string {
		return pointer.Of("default value from a lambda")
	})

	_ = zerokit.Init(&mt.V, pointer.Of(pointer.Of("default value from a pointer")))
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

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