zerokit

package
v0.298.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2025 License: Apache-2.0 Imports: 7 Imported by: 4

README

package zeroutil

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

package mypkg

import (
	"go.llib.dev/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 (
	"go.llib.dev/frameless/pkg/zerokit"
)

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

func Init

func Init[T any, I *T | func() T](ptr *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 (
	"go.llib.dev/frameless/pkg/zerokit"
)

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

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

	var defaultValue = "default value from a shared variable"
	_ = zerokit.Init(&mt.V, &defaultValue)
}

func InitErr added in v0.266.0

func InitErr[T any](ptr *T, init func() (T, error)) (T, error)

InitErr 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 function. InitErr is safe to use concurrently, it has no race condition.

If the initialiser function encounters an InitErr failure, it will leave the provided *T pointer unassigned, allowing subsequent calls to attempt initialization again.

Example
package main

import (
	"fmt"

	"go.llib.dev/frameless/pkg/zerokit"
)

func main() {
	var val string
	got, err := zerokit.InitErr(&val, func() (string, error) {
		return "foo", fmt.Errorf("some error might occur, it will be handled")
	})
	_, _ = got, err
}

func InitErrWith added in v0.292.0

func InitErrWith[T any, L *sync.RWMutex | *sync.Mutex](ptr *T, l L, init func() (T, error)) (T, error)

func InitWith added in v0.292.0

func InitWith[T any, L *sync.RWMutex | *sync.Mutex | *sync.Once](ptr *T, l L, init func() T) T

func IsZero added in v0.206.0

func IsZero[T any](v T) (ok bool)

IsZero will report whether the value is zero or not.

Types

type V added in v0.214.0

type V[T any] struct {
	// contains filtered or unexported fields
}

V is a type that can initialise itself upon access (V.Get). Map, Slice, Chan types are made, while primitive types returned as zero value. Pointer types are made with an initialised value.

V is not thread safe, it just makes initialisation at type level in struct fields more convenient. The average cost for using V is low, see the benchmark for more

Example
package main

import (
	"go.llib.dev/frameless/pkg/zerokit"
)

func main() {
	type MyStructType struct {
		fields    zerokit.V[*[]string]
		foundKeys zerokit.V[map[string]struct{}]
	}

	var mst MyStructType
	mst.foundKeys.Get()
}

func (*V[T]) Get added in v0.214.0

func (i *V[T]) Get() T

func (*V[T]) Ptr added in v0.214.0

func (i *V[T]) Ptr() *T

func (*V[T]) Set added in v0.214.0

func (i *V[T]) Set(v T)

Jump to

Keyboard shortcuts

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