Giter Site home page Giter Site logo

go-result's Introduction

go-result Experimental

This is an attempt to implement the rust Result<T, Error> Type in go. This is purely experimental. Do not use it in production.

Use Case

if you define a method ReadFile that returns a Result? eg

func ReadFile(input string) result.Result[[]string] {
	file, err := ioutil.ReadFile(input)
	if err != nil {
		return result.Error[[]string](err)
	}
	fileContent := string(file)
	return result.Ok(strings.Split(fileContent, "\n"))
}

You can read the contents of a file using

for l := range ReadFile("./rick-roll-1").Result() {
  fmt.Println(l)
}

/* 
Output:  

Never gonna give you up
Never gonna let you down
Never gonna run around and desert you
*/

You can also chain results like this

song := ReadFile("./rick-roll-1").
  Then(func(p1 []string) result.Resolver[[]string] {
    return result.Ok(append(p1, ReadFile("./rick-roll-2").Result()...))
  }).Result()


/* 
Output:

Never gonna give you up
Never gonna let you down
Never gonna run around and desert you

Never gonna make you cry
Never gonna say goodbye
Never gonna tell a lie and hurt you
*/

go-result's People

Contributors

bubunyo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.