GithubHelp home page GithubHelp logo

result's Introduction

result

result is a package that naively implements Result<T, E> from Rust in Go.

It is used for returning and propagating values and errors.

Installation

Install with go mod

go get github.com/pijng/result

Usage/Examples

Creating a new Result object and returning it as the function's result:

type User struct {
  name string
  age  int
}

func getUser(id int) result.Result[User] {
  if id == 0 {
    return result.New(User{}, fmt.Errorf("user with id %d not found", id))
  }

  user := User{name: "pijng", age: 26}

  return result.New(user, nil)
}

func main() {
  r1 := getUser(1)
  r2 := getUser(0)

  fmt.Println(r1.Value()) // outputs: {pijng 26}
  fmt.Println(r1.Error()) // outputs: <nil>
  fmt.Println(r2.Value()) // outputs: { 0}
  fmt.Println(r2.Error()) // outputs: user with id 0 not found
}

Usage with pattern matching:

type User struct {
  name string
  age  int
}

func getUser(id int) result.Result[User] {
  if id == 0 {
    return result.New(User{}, fmt.Errorf("user with id %d not found", id))
  }

  user := User{name: "pijng", age: 26}

  return result.New(user, nil)
}

func main() {
    r1 := getUser(1)

    switch r1.Match() {
    case result.Ok(): // This block will always be called.
        fmt.Println(r1.Value())
    case result.Err(): // This block will never be called.
        fmt.Println(r1.Error())
    }

    r2 := getUser(0)

    switch r2.Match() {
    case result.Ok(): // This block will never be called.
        fmt.Println(r2.Value())
    case result.Err(): // This block will always be called.
        fmt.Println(r2.Error())
    }
}

Unwrap value and error from Result[T]:

func getUser(id int) result.Result[User] {
  if id == 0 {
    return result.New(User{}, fmt.Errorf("user with id %d not found", id))
  }

  user := User{name: "pijng", age: 26}

  return result.New(user, nil)
}

func main() {
    r1 := getUser(1)

    user, err := r1.Unwrap()

    fmt.Println(user) // outputs: {pijng 26}
    fmt.Println(err) // outputs: <nil>

    r2 := getUser(0)

    user, err := r2.Unwrap()

    fmt.Println(user) // outputs: { 0}
    fmt.Println(err) // outputs: user with id 0 not found
}

FAQ

Why do that in the first place?

Honestly, it doesn't make much sense.

In the end, working with Result[T] boils down to either simulating pattern matching, which doesn't differ much from a regular if err != nil, or propagating data up the layers, which is also similar to the standard return (T, error) signature.

This package was created out of curiosity. While it serves its function of mimicking Result<T, E>, it doesn't bring any significant advantages in itself.

Can this package be used in production?

It's better not to. On one hand, everything works correctly, but on the other hand, you will introduce unnecessary complexity to the project.

But in general, you can :)

License

MIT

result's People

Contributors

pijng avatar

Stargazers

 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.