Maybe is a Go package to provide basic functionality for Option type structures

Maybe

Go Report Card CI Coverage Status

Maybe is a library that adds an Option data type for some native Go types.

What does it offer:

The types exported by this library are immutable and thread safe. The json serialization and deserialization works in the same way as with the native types. Using this library will free you up from using pointers and possible panics.

It also gets rid of the situations where an absence of value means something different from a default (zero) value. For example: a person with salary 100 means he/she has a paid job, 0 means an unpaid internship and null means unemployed. Supporting yourself with Option eliminates the usage of null replacing it with HasValue.

When should I use it:

It can be used for transport layer (as it has json capabilities) but it could also be used on the domain layer.

Examples:

Marshal of String Option without value

package main

import (
	"encoding/json"
	"fmt"

	"github.com/pmorelli92/maybe"
)

type Person struct {
	Name maybe.String `json:"name"`
	Age  int          `json:"age"`
}

func main() {
	p := Person{Age: 28}
	bytes, _ := json.Marshal(p)
	fmt.Println(string(bytes)) // {"name":null,"age":28}
}

Marshal of String Option with value

package main

import (
	"encoding/json"
	"fmt"

	"github.com/pmorelli92/maybe"
)

type Person struct {
	Name maybe.String `json:"name"`
	Age  int          `json:"age"`
}

func main() {
	p := Person{Age: 28, Name: maybe.SetString("Pablo")}
	bytes, _ := json.Marshal(p)
	fmt.Println(string(bytes)) // {"name":"Pablo","age":28}
}

Unmarshal of String Option without value

package main

import (
	"encoding/json"
	"fmt"

	"github.com/pmorelli92/maybe"
)

type Person struct {
	Name maybe.String `json:"name"`
	Age  int          `json:"age"`
}

func main() {
	var p Person
	_ = json.Unmarshal([]byte(`{"age":28}`), &p)
	fmt.Println(p.Name.HasValue()) // false
}

Unmarshal of String Option with value

package main

import (
	"encoding/json"
	"fmt"

	"github.com/pmorelli92/maybe"
)

type Person struct {
	Name maybe.String `json:"name"`
	Age  int          `json:"age"`
}

func main() {
	var p Person
	_ = json.Unmarshal([]byte(`{"age":28, "name": "Pablo"}`), &p)
	fmt.Println(p.Name.HasValue()) // true
	fmt.Println(p.Name.Value())    // Pablo
}

Types supported:

  • bool
  • string
  • float
  • int
  • time

If this library is not supporting certain type, feel free to do a pull request or add an issue asking for it.

Generics

Go does not support generics as of now, but the draft was recently approved. When they become available on Go 1.18 this library will be updated and only a generic struct will remain. The library will look like this: go2playgrounds.

Owner
Pablo Morelli
C# and Go developer. <3 Kubernetes.
Pablo Morelli
Similar Resources

Yet another Go package for working with *.golden test files, with a focus on simplicity.

go-golden Yet another Go package for working with *.golden test files, with a focus on simplicity. Import import "github.com/jimeh/go-golden" Usage fu

Aug 3, 2022

Atomic: a go package for atomic file writing

atomic import "github.com/natefinch/atomic" atomic is a go package for atomic file writing By default, writing to a file in go (and generally any lan

Nov 10, 2021

Go package for tail-ing files

Go package for tail-ing files A Go package striving to emulate the features of the BSD tail program. t, err := tail.TailFile("/var/log/nginx.log", tai

Dec 8, 2021

CSV asynchronous I/O package for go.

golib/csvio selectでの非同期処理に対応した、csvベースでの読み込み/書き込みを行うライブラリです。 csvio.Config CSVのコンマの値や、クォートの有無など、扱うCSVに対するフォーマットを指定できます。 csvio.Reader selectでの非同期処理に対応した、

Jan 21, 2022

atomic time package with json Marshal / Unmarshal support

ATime Atomic Time package for Go, optimized for performance yet simple to use. Usage // one line create dt := atime.New() // allocates *AtomicTime dt

Feb 6, 2022

[TOOL, CLI] - Filter and examine Go type structures, interfaces and their transitive dependencies and relationships. Export structural types as TypeScript value object or bare type representations.

typex Examine Go types and their transitive dependencies. Export results as TypeScript value objects (or types) declaration. Installation go get -u gi

Dec 6, 2022

Package create provides a generic option pattern for creating new values of any type

create Package create provides a generic option pattern for creating new values

Dec 30, 2021

Nat-type-identifier-go - A Go based implementation of Network Address Transalation (NAT) type identifier based on nat-type-identifier

nat-type-identifier-go A Go based implementation of Network Address Transalation

May 8, 2022

Go-postgres - go-postgres library provide NoSQL functionality which can execute queries with pool of connections

GO Postgres go-postgres library provide NoSQL functionality which can execute queries with pool of connections. What is this repository for? Establish

Dec 31, 2021

A go1.18+ package to (maybe) simplify performing operations on slices in a fluent-like style.

sop ✨ W.I.P. ✨ sop (slices operation) is a go1.18+ package to (maybe) simplify performing operations on slices in a fluent-like style with common oper

Oct 1, 2022

Package socket provides a low-level network connection type which integrates with Go's runtime network poller to provide asynchronous I/O and deadline support. MIT Licensed.

socket Package socket provides a low-level network connection type which integrates with Go's runtime network poller to provide asynchronous I/O and d

Dec 14, 2022

Basic LDAP v3 functionality for the GO programming language.

Basic LDAP v3 functionality for the GO programming language. Install For the latest version use: go get gopkg.in/ldap.v2 Import the latest version wi

May 24, 2022

Basic LDAP v3 functionality for the GO programming language.

Basic LDAP v3 functionality for the GO programming language. The library implements the following specifications: https://tools.ietf.org/html/rfc4511

Dec 31, 2022

Transformations for 2D and 3D(maybe)

barnacle The Name What a name right? When creating this repo Github showed overland-barnacle. Just had to use barnacle! What is it? Transformations fo

Dec 28, 2021

Using finite projective planes to make card (maybe video) games

pairwise What it is Using finite projective plane to generate card (maybe video) games. Running Run with go run . Right now uses Go 1.17 but 1.18 just

Jan 24, 2022

A patcher for the MS-DOS version of Fallout 1 including Crafty's sFall1 patches and maybe more

A patcher for the MS-DOS version of Fallout 1 including Crafty's sFall1 patches and maybe more

Oct 13, 2022

Impress your friends with your ability to maybe solve the Wordle most of the time (probably)

wordle-assistant Impress your friends with your ability to maybe solve the Wordle most of the time (probably). This was coded as quickly and dirtily a

Feb 10, 2022

A Slippy Map written in Go, eventually destined to be compiled to WebAssembly, for the plane.watch frontend (maybe).

plane-watch/pw-slippymap A Slippy Map written in Go, runs in desktop mode or in js/wasm, for the plane.watch frontend (maybe). Current state Loads as

May 5, 2022

Null Types, Safe primitive type conversion and fetching value from complex structures.

Typ Typ is a library providing a powerful interface to impressive user experience with conversion and fetching data from built-in types in Golang Feat

Sep 26, 2022
Related tags
Provide an upload endpoint that stores files on pinata and returns a json response with the uploaded file pinata url

Purpose Build a template repository to get to coding as quickly as possible, by starting from a common template which follows the guidelines here Feat

Dec 30, 2021
Recreate embedded filesystems from embed.FS type in current working directory.

rebed Recreate embedded filesystems from embed.FS type in current working directory. Expose the files you've embedded in your binary so users can see

Sep 27, 2022
Add a type for paths in Go.

pathtype Treat paths as their own type instead of using strings. This small package wraps functions from the standard library to create a new Path typ

Sep 26, 2022
A basic file server automatically generates self certificates and serves the given folder.

A basic file server automatically generates self certificates and serves the given folder.

Jul 20, 2022
A package to allow one to concurrently go through a filesystem with ease

skywalker Skywalker is a package to allow one to concurrently go through a filesystem with ease. Features Concurrency BlackList filtering WhiteList fi

Nov 14, 2022
Package cae implements PHP-like Compression and Archive Extensions.

Compression and Archive Extensions 中文文档 Package cae implements PHP-like Compression and Archive Extensions. But this package has some modifications de

Jun 16, 2022
mateors zip and unzip package

Zip unzip package How to install this package? go get github.com/mateors/mzip How to make zip files? package main import "github.com/mateors/mzip" f

Apr 25, 2022
A Go filesystem package for working with files and directories

Stowage A Go filesystem package for working with files and directories, it features a simple API with support for the common files and directories ope

May 28, 2021
gsheet is a CLI tool (and Golang package) for piping csv data to and from Google Sheets

gsheet Table of Contents Introduction Why? Installation Authentication and Authorization What about OAuth authentication? CLI Usage Sheet commands Dri

Nov 15, 2022
noioutil finds files using the "io/ioutil" package.

noioutil noioutil finds files using the "io/ioutil" package.

Dec 28, 2021