alloc

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 8, 2023 License: MIT Imports: 2 Imported by: 0

README

alloc

Allocation and dereference package for Go.

Requirements

Go 1.20 and above.

This package based on generics feature introduced for Go 1.18, but tested with Go 1.20 only.

Example

See the examples_test.go for a small example of how to use the package.

Documentation

Overview

Package alloc is intended to help make pointers to specified values with single call and also to get values from pointers safely to avoid nil dereference errors.

Example (Use)
package main

import (
	"fmt"
	"time"

	"github.com/wtask-go/alloc"
)

// fictional data model
type model struct {
	Value    *float64
	Modified *time.Time
}

func (m *model) Print() {
	if m == nil {
		fmt.Println("model: <nil>")
		return
	}

	fmt.Printf("model: {%v, %v}\n", alloc.Value(m.Value), alloc.Value(m.Modified))
}

const defaultValue = 3.14

func main() {
	m := &model{
		Value: alloc.New(defaultValue), // useful referencing to constant values
	}

	m.Print()

	m.Value = alloc.New(alloc.Value(m.Value) * 2) // inline operations with referenced values
	m.Modified = alloc.New(time.Date(2023, 5, 8, 15, 0, 0, 0, time.UTC))

	m.Print()

	val := alloc.Copy(m.Value)
	*val = defaultValue // m.value and val are referenced to different values
	fmt.Printf("m.value: %v, val: %v\n", alloc.Value(m.Value), alloc.Value(val))

	m.Value = nil
	if _, ok := alloc.Deref(m.Value); !ok {
		// synthetic case due to you able to directly check m.Value == nil
		fmt.Println("update is not required: nil value")
	}

	// pipelined computation with pointers

	if v := alloc.Value[float64](nil); v != 0 {
		fmt.Println("division result:", defaultValue/v)
	} else {
		fmt.Println("invalid division by zero")
	}

	fmt.Println("subtraction:", defaultValue-alloc.Value[float64](nil))

}
Output:

model: {3.14, 0001-01-01 00:00:00 +0000 UTC}
model: {6.28, 2023-05-08 15:00:00 +0000 UTC}
m.value: 6.28, val: 3.14
update is not required: nil value
invalid division by zero
subtraction: 3.14

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy

func Copy[V Scope](ref *V) *V

Copy is generic function to duplicate specified pointer with referenced value. Returns new pointer for specified non nil reference or nil otherwise.

func Deref

func Deref[V Scope](ref *V) (V, bool)

Deref is generic function to dereference a pointer. Returns referenced value and true for non nil pointer or type zero value and false otherwise.

func New

func New[V Scope](value V) *V

New is generic function to allocate new pointer to specified value.

func Value

func Value[V Scope](ref *V) V

Value is generic function to dereference a pointer. Returns referenced value for non nil pointer or type zero value otherwise.

Types

type Scope

type Scope interface {
	constraints.Ordered | ~bool | time.Time | time.Duration
}

Scope represents type constraints to allow generalized operations within this package.

Jump to

Keyboard shortcuts

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