box

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 29, 2026 License: MIT Imports: 0 Imported by: 0

README ¶

box

box is a minimalist, type-safe utility package for Go that introduces generic container types to safely handle the presence or absence of data without relying on risky nil pointer dereferences.

By leveraging Go generics, box provides a clean, expressive API inspired by functional programming patterns like Optional and Maybe.

🚀 Quick Start

package main

import (
	"fmt"
	"github.com/atendi9/box"
)

func main() {
	// Creating a present value
	name := box.NewSome("Capivara")
	if name.IsPresent() {
		fmt.Println("Hello,", name.Get()) // Output: Hello, Capivara
	}

	// Creating an empty value
	empty := box.NewNone[string]()
	fmt.Println("Value:", empty.Get()) // Output: Value: "" (returns zero value safely)
}

Documentation ¶

Index ¶

Constants ¶

This section is empty.

Variables ¶

This section is empty.

Functions ¶

This section is empty.

Types ¶

type None ¶

type None[T any] struct{}

None represents an Optional instance that does not contain any value.

func (*None[T]) Get ¶

func (n *None[T]) Get() T

Get always returns the zero value of type T since None contains no value.

func (*None[T]) IsEmpty ¶

func (n *None[T]) IsEmpty() bool

IsEmpty always returns true as None represents the absence of a value.

func (*None[T]) IsPresent ¶

func (n *None[T]) IsPresent() bool

IsPresent always returns false as None never contains a value.

type Optional ¶

type Optional[T any] interface {
	// IsPresent returns true if there is a value present, otherwise false.
	IsPresent() bool

	// IsEmpty returns true if there is no value present, otherwise false.
	IsEmpty() bool

	// Get returns the value if present. If no value is present, it returns the zero value of type T.
	Get() T
}

Optional represents a container object which may or may not contain a non-nil value. It provides a type-safe alternative to using nil pointers.

func NewNone ¶

func NewNone[T any]() Optional[T]

NewNone creates and returns a new Optional representing an empty value. The returned instance is a pointer to None.

func NewSome ¶

func NewSome[T any](value T) Optional[T]

NewSome creates and returns a new Optional containing the provided value. The returned instance is a pointer to Some.

type Some ¶

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

Some represents an Optional instance that contains a value.

func (*Some[T]) Get ¶

func (s *Some[T]) Get() T

Get returns the underlying value. If the receiver is nil, it returns the zero value of type T.

func (*Some[T]) IsEmpty ¶

func (s *Some[T]) IsEmpty() bool

IsEmpty returns true if the receiver is nil.

func (*Some[T]) IsPresent ¶

func (s *Some[T]) IsPresent() bool

IsPresent returns true if the receiver is not nil.

type Void ¶

type Void = struct{}

Void represents an empty structure used to signal the absence of data. It is commonly used for set implementations or synchronization signals.

var NULL Void

NULL is a globally shared instance of Void.

Jump to

Keyboard shortcuts

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