nul

package module
v0.0.0-...-2ce86a1 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 7 Imported by: 3

README

go-nul

Package nul implements a nullable optional-type, for the Go programming language.

In other programming languages, an optional-type might be known as: a option-type, or a maybe-type.

Documention

Online documentation, which includes examples, can be found at: http://godoc.org/github.com/reiver/go-nul

GoDoc

Examples

Here is an example nullable optional-type that can hold a string:

import "github.com/reiver/go-nul"

//

var op nul.Nullable[string] // the default value is nothing.

// ...

if nul.Nothing[string]() == op {
	fmt.Println("nothing")
}

// ...

op = nul.Null[string]()

// ...

if nul.Null[string]() == op {
	fmt.Println("null")
}

// ...

op = nul.Something("Hello world! 👾")

// ...

switch op {
case op.Nothing[string]():
	//@TODO
case op.Null[string]():
	//@TODO
case op.Something("apple"):
	//@TODO
case op.Something("banana"):
	//@TODO
case op.Something("cherry"):
	//@TODO
default:
	//@TODO
}

// ...

value, found := op.Get()
if found {
	fmt.Println("VALUE:", value)
} else {
	fmt.Println("either nothing or null")
}

Import

To import package nul use import code like the follownig:

import "github.com/reiver/go-nul"

Installation

To install package nul do the following:

GOPROXY=direct go get github.com/reiver/go-nul

Author

Package nul was written by Charles Iliya Krempeaux

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Nullable

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

func Nothing

func Nothing[T any]() Nullable[T]

func Null

func Null[T any]() Nullable[T]

func Something

func Something[T any](value T) Nullable[T]

func (Nullable[T]) Filter

func (receiver Nullable[T]) Filter(fn func(T) bool) Nullable[T]

func (Nullable[T]) Get

func (receiver Nullable[T]) Get() (T, bool)

Get returns the value inside of the nullable-optional-type if it is holding something.

Example usage:

var nl nul.Nullable[string]

// ...

value, found := nl.Get()

if found {
	fmt.Println("VALUE:", value)
} else {
	fmt.Println("nothing")
}

func (Nullable[T]) GetElse

func (receiver Nullable[T]) GetElse(alternative T) T

GetElse returns the value inside of the nullable-optional-type if it is holding something. Else it returns the alternstive value passed as a parameter. Example usage:

var nl nul.Nullable[string]

// ...

value := nl.GetElse(alternative)

fmt.Println("VALUE:", value)

func (Nullable[T]) GoString

func (receiver Nullable[T]) GoString() string

func (Nullable[T]) IsNothing

func (receiver Nullable[T]) IsNothing() bool

func (Nullable[T]) IsNull

func (receiver Nullable[T]) IsNull() bool

func (Nullable[T]) MarshalJSON

func (receiver Nullable[T]) MarshalJSON() ([]byte, error)

MarshalJSON makes it so json.Marshaler is implemented.

func (Nullable[T]) Optional

func (receiver Nullable[T]) Optional() opt.Optional[T]

func (*Nullable[T]) UnmarshalJSON

func (receiver *Nullable[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON makes it so json.Unmarshaler is implemented.

func (Nullable[T]) WhenNothing

func (receiver Nullable[T]) WhenNothing(fn func())

func (Nullable[T]) WhenNull

func (receiver Nullable[T]) WhenNull(fn func())

func (Nullable[T]) WhenSomething

func (receiver Nullable[T]) WhenSomething(fn func(T))

Jump to

Keyboard shortcuts

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