andeya / option

Go-generics option module inspired by rust.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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.

License:MIT License


Languages

Language:Go 100.0%