Code Monkey home page Code Monkey logo

goresult's Introduction

goresult

goresult is a Golang library that provides Result and Option types, similar to those used in functional programming, to handle errors and optional values in a structured and expressive way.

Introduction

The goresult library introduces Result and Option types, which are commonly used in functional programming, to simplify error handling and deal with optional values more effectively. These types offer a clean and concise approach to manage success and failure cases, as well as situations where a value may or may not exist.

Features

  • Result Type: The Result type represents a value that can either be a success or an error. It enables you to handle potential errors without resorting to traditional error handling techniques, improving code readability and maintainability.

  • Option Type: The Option type represents an optional value that may or may not be present. It allows you to handle scenarios where a value may be absent, eliminating the need for explicit nil checks and reducing the chance of null pointer errors.

Installation

To use goresult in your Go project, you can import it using the following command:

go get github.com/siriusa51/goresult

Usage

Here's a simple example demonstrating the usage of Result and Option types:

package main

import "github.com/siriusa51/goresult"

type KV struct {
	Key   string
	Value string
}

func main() {
	r1 := goresult.Ok(KV{Key: "key", Value: "value"})
	println(r1.IsOk())
	// -> true
	println(r1.IsError())
	// -> false

	r2 := goresult.Error[int]("error")
	println(r2.IsOk())
	// -> false
	println(r2.IsError())
	// -> true

	o := goresult.Some(KV{Key: "key", Value: "value"})
	println(o.IsSome())
	// -> true
	println(o.IsNone())
	// -> false

	n := goresult.None[KV]()
	println(n.IsSome())
	// -> false
	println(n.IsNone())
	// -> true
}

For more examples, you can refer to the comments within the code.

Contributing

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request on the GitHub repository.

License

This project is licensed under the MIT License.

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.