Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.
/ option Public archive

Go-generics option module inspired by rust.

License

Notifications You must be signed in to change notification settings

andeya/option

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages