Skip to content
This repository was archived by the owner on Apr 16, 2022. It is now read-only.

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

option Docs

Go-generics option module inspired by rust.

Avoid nil value, handle value with Option type, will you choose her?

Note: Migrated to gust.

Go Version

go≥1.18

Example

  • Option
func ExampleOption() {
	type A struct {
		X int
	}
	var a = Some(A{X: 1})
	fmt.Println(a.IsSome(), a.IsNone())

	var b = None[A]()
	fmt.Println(b.IsSome(), b.IsNone())

	var x = b.UnwrapOr(A{X: 2})
	fmt.Println(x)

	type B struct {
		Y string
	}
	var c = Map(a, func(t A) B {
		return B{
			Y: strconv.Itoa(t.X),
		}
	})
	fmt.Println(c)

	// Output:
	// true false
	// false true
	// {2}
	// Some({1})
}
  • Optnil
func ExampleOptnil() {
	type A struct {
		X int
	}
	var a = Ptr(&A{X: 1})
	fmt.Println(a.NotNil(), a.IsNil())

	var b = Nil[A]()
	fmt.Println(b.NotNil(), b.IsNil())

	var x = b.UnwrapOr(&A{X: 2})
	fmt.Println(x)

	type B struct {
		Y string
	}
	var c = OptnilMap(a, func(t *A) *B {
		return &B{
			Y: strconv.Itoa(t.X),
		}
	})
	fmt.Println(c)

	// Output:
	// true false
	// false true
	// &{2}
	// NonNil(&{1})
}

About

Go-generics option module inspired by rust.

Topics

Resources

Stars

4 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages