Skip to content

heat1q/opt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Option

Go Doc GitHub tag (latest by date) Go Report Card Software License

This package implements a Rust styled Option type for optional values in Go. Option provides a wrapper around a value that may or may not be initialized and a set of methods to extract the inner value or handle nil cases.

Usage

Installation

go get -u github.com/heat1q/opt

Example

package main

import (
	"fmt"

	"github.com/heat1q/opt"
)

func main() {
	o := opt.New("potato")
	value, ok := o.Some()
	fmt.Println(ok) // true
	fmt.Println(value) // potato
}

Marshalling JSON

Option solves the nullable issue for values where the default of a value is also considered valid. For instance, consider the scenario where the false value of a bool is a valid instance of the nullable field.

package main

import (
	"fmt"

	"github.com/heat1q/opt"
)

type data struct {
	Flag opt.Option[bool] `json:"flag,omitempty"`
}

func main() {
	var d data

	_ = json.Unmarshal(`{}`, &d)
	_, ok := d.Value.Some() 
	fmt.Println(ok) // false
	
	_ = json.Unmarshal(`{"flag": false}`, &d)
	_, ok = d.Value.Some() 
	fmt.Println(ok) // true
}