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

Go-generics result module inspired by rust.

License

Notifications You must be signed in to change notification settings

andeya/result

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

result Docs

Go-generics result module inspired by rust.

Avoid ifelse, handle result with chain methods, will you choose her?

Note: Migrated to gust.

Go Version

go≥1.18

Example

func ExampleAndThen() {
	var divide = func(i, j float32) Result[float32] {
		if j == 0 {
			return Err[float32]("j can not be 0")
		}
		return Ok(i / j)
	}
	var ret float32 = divide(1, 2).AndThen(func(i float32) Result[float32] {
		return Ok(i * 10)
	}).Unwrap()
	fmt.Println(ret)
	// Output:
	// 5
}
func ExampleDefaultValue() {
	const def int = 10

	// before
	i, err := strconv.Atoi("1")
	if err != nil {
		i = def
	}
	fmt.Println(i * 2)

	// now
	fmt.Println(Wrap(strconv.Atoi("1")).UnwrapOr(def) * 2)

	// Output:
	// 2
	// 2
}

About

Go-generics result module inspired by rust.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages