Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

prasad83/goroe

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-roe

Golang pattern to return Result or Error useful when array of values need to be returned from the function.

Inspired from Rust Result unwrap

Usage

Simple

import (
    "github.com/prasad83/goroe"
)

func helloWorld() ResultOrError[string] {
    return goroe.Result("hello world")
}

func main() {
    if result, err := helloWorld().Unwrap(); err != nil {
        panic(err)
    } else {
        println(result)
    }
}

Complex

import (
    "github.com/prasad83/goroe"
)

func helloPlanets(planets ...string) []ResultOrError[string] {
	returns := make([]ResultOrError, len(planets))
	for index, planet := range planets {
		switch strings.ToUpper(planet) {
		case "MERCURY":
			fallthrough
		case "VENUS":
			fallthrough
		case "EARTH":
			fallthrough
		case "MARS":
			fallthrough
		case "JUPITER":
			fallthrough
		case "SATURN":
			fallthrough
		case "NEPTUNE":
			fallthrough
		case "PLUTO":
			returns[index] = Result[string](fmt.Sprintf("Hello %s", planet))
		default:
			returns[index] = Errorf[string]("Not a planet")
		}
	}
	return returns
}

func main() {
	results := helloPlanets("Earth", "Moon")
	for _, roe := range results {
		if r, err := roe.Unwrap(); err != nil {
			println("Error: " + err.Error())
		} else {
			println(r)
		}
	}
}

About

Golang return Result or Error Pattern

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.