An experimental Go package that allows you to write less error handling and makes the logic look simpler and clearer

Overview


go-ez


Table of Contents
  1. About
  2. Why go-ez?
  3. Goals
  4. Getting Started
  5. Roadmap
  6. Contributing
  7. License
  8. Contact

About

go-ez is an experimental Go package that allows you to write less error handling and makes the logic look simpler and clearer.

This project is still in its early stages, so any thoughts and feedback are very welcome!

(back to top)

Why go-ez?

Many Go developers get tired of dealing with errors because there are too many errors to handle one by one, which is intuitive and effective, but really annoying.

I've been trying to find out if Go has an error handling method like try/catch, I think that would probably be a lot easier, but unfortunately, I couldn't find any package that's easy to use.

So I tried to make one myself, taking inspiration from the try/catch syntax, but not wanting to lose the Go error handling style, and then go-ez was born!

Be Careful

Sometimes native Go syntax is simpler and better. go-ez is just a package for simplify error handling in parts of your code, not for replacing Go's native syntax.

If you have any issue using go-ez, it's easy to convert to native syntax at any time, and don't forget to share your thoughts with us! Thanks!

(back to top)

Goals

Yeah, it's not ez at all.

  • Allow developers to write less error handling, but achieve the same result.
  • Makes the logics look simpler and clearer.
  • Make it easy to use existing library and helper functions.
  • Make it easy to migrate from or your previous projects, vice versa.
  • Make the logics reusable.

(back to top)

Getting Started

Installation

go get github.com/ez4o/go-ez

Usage

  1. Simply pass anything to Got[T, U](). Remember to specify input type and output type.

  2. Write what you want to do in Then(). Wrap each of them with the functions below.

  3. Handle the returned pair of (value, error) from Got(...).Then(...).Done()!

Wrap Functions

Function Name Description
Try() The most frequently used one. It takes func(T) (U, error).
Do() Takes func(T) (U).
Make() Takes func(T) (error).
Void() Takes func(T).

Example

In the past, if you wanted to fetch JSON from a url and unmarshal it, you might write it like this:

import (
  "encoding/json"
  "fmt"
  "io/ioutil"
  "net/http"
)

// Existing helper function.
func readBytesFromResponse(response *http.Response) ([]byte, error) {
  defer response.Body.Close()
  return ioutil.ReadAll(response.Body)
}

// Existing helper function.
func unmarshal(bytes []byte) error {
  var data map[string]interface{}

  err := json.Unmarshal(bytes, &data)
  if err != nil {
    return nil, fmt.Errorf("Something goes wrong!")
  }

  return data, nil
}

func main() {
  url := "https://jsonplaceholder.typicode.com/posts"
  
  resp, err := http.Get(url)
  if err != nil {
    panic(err)
  }

  bytes, err := readBytesFromResponse(resp)
  if err != nil {
    panic(err)
  }

  data, err := unmarshal(bytes)
  if err != nil {
    panic(err)
  }
  
  fmt.Println(data)
}

But, with go-ez:

import (
  "encoding/json"
  "fmt"
  "io/ioutil"
  "net/http"

  . "github.com/ez4o/go-ez"
)

// Existing helper function.
func readBytesFromResponse(response *http.Response) ([]byte, error) {
  defer response.Body.Close()
  return ioutil.ReadAll(response.Body)
}

// Existing helper function.
func unmarshal(bytes []byte) error {
  var data map[string]interface{}

  err := json.Unmarshal(bytes, &data)
  if err != nil {
    return nil, fmt.Errorf("Something goes wrong!")
  }

  return data, nil
}

func main() {
  // Remember to specify input type and output type.
  data, err := Got[string, map[string]interface{}](
    "https://jsonplaceholder.typicode.com/posts",
  ).Then(
    Try(http.Get),
    Try(readBytesFromResponse),
    Try(unmarshal),
  ).Done()

  if err != nil {
    panic(err)
  }

  fmt.Println(data)
}

(back to top)

Roadmap

  • Handle functions that have no parameters but return values.

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feat/amazing-feature)
  3. Commit your Changes with Conventional Commits
  4. Push to the Branch (git push origin feat/amazing-feature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

Author

Project Link

(back to top)

Owner
ez4o
Create awesome tools that are easy for all user to use.
ez4o
Simple error handling primitives

errors Package errors provides simple error handling primitives. go get github.com/pkg/errors The traditional error handling idiom in Go is roughly ak

null 7.6k Jan 22, 2022
A comprehensive error handling library for Go

Highlights The errorx library provides error implementation and error-related utilities. Library features include (but are not limited to): Stack trac

Joom 795 Jan 17, 2022
Declarative error handling for Go.

ErrorFlow Declarative error handling for Go. Motivation Reading list: Don't defer Close() on writable files Error Handling — Problem Overview Proposal

Serhiy T 9 Mar 28, 2021
brief: a piece of error handling codelet

brief a piece of error handling codelet. this code only demonstrates how to hide sql.ErrNoRows to the caller. the Get() method defined in the pkg/proj

null 0 Oct 30, 2021
Just another error handling primitives for golang

errors Just another error handling primitives for golang Install go install github.com/WAY29/[email protected] Usage New error and print error context Th

Longlone 4 Oct 9, 2021
🥷 CError (Custom Error Handling)

?? CError (Custom Error Handling) Installation Via go packages: go get github.com/rozturac/cerror Usage Here is a sample CError uses: import ( "gi

Rıdvan ÖZTURAÇ 1 Dec 5, 2021
Errors - A lib for handling error gracefully in Go

?? Errors Errors 是一个用于优雅地处理 Go 中错误的库。 Read me in English ??‍ 功能特性 优雅地处理 error,嗯,

水不要鱼 1 Jan 17, 2022
Error handling hook & helper function to simplify writing API handler methods in Go.

Error handling hook & helper function to simplify writing API handler methods in Go.

Steven Frew 0 Jan 19, 2022
Wraps the normal error and provides an error that is easy to use with net/http.

Go HTTP Error Wraps the normal error and provides an error that is easy to use with net/http. Install go get -u github.com/cateiru/go-http-error Usage

Yuto Watanabe 0 Dec 20, 2021
Go error library with error portability over the network

cockroachdb/errors: Go errors with network portability This library aims to be used as a drop-in replacement to github.com/pkg/errors and Go's standar

CockroachDB 1.3k Jan 12, 2022
This structured Error package wraps errors with context and other info

RErr package This structured Error package wraps errors with context and other info. It can be used to enrich logging, for example with a structured l

Rohan Allison 0 Jan 21, 2022
A Go (golang) package for representing a list of errors as a single error.

go-multierror go-multierror is a package for Go that provides a mechanism for representing a list of error values as a single error. This allows a fun

HashiCorp 1.5k Jan 17, 2022
A powerful, custom error package for Go

custom-error-go A powerful, custom error package for Go Detailed explanation: https://medium.com/codealchemist/error-handling-in-go-made-more-powerful

Abhinav Bhardwaj 7 Dec 27, 2021
Reduce debugging time while programming Go. Use static and stack-trace analysis to determine which func call causes the error.

Errlog: reduce debugging time while programming Introduction Use errlog to improve error logging and speed up debugging while you create amazing code

Martin Joly 400 Dec 21, 2021
Error tracing and annotation.

errors -- import "github.com/juju/errgo" The errors package provides a way to create and diagnose errors. It is compatible with the usual Go error idi

Juju 231 Jan 4, 2022
Error interface wrappers for Google's errdetails protobuf types, because they're handy as heck and I want to use them more

Error interface wrappers for Google's errdetails protobuf types, because they're handy as heck and I want to use them more

Claudia Hardman 1 Nov 18, 2021
A drop-in replacement for Go errors, with some added sugar! Unwrap user-friendly messages, HTTP status code, easy wrapping with multiple error types.

Errors Errors package is a drop-in replacement of the built-in Go errors package with no external dependencies. It lets you create errors of 11 differ

Kamaleshwar 26 Jan 16, 2022
A flexible error support library for Go

errors Please see http://godoc.org/github.com/spacemonkeygo/errors for info License Copyright (C) 2014 Space Monkey, Inc. Licensed under the Apache Li

Space Monkey Go 167 Jul 6, 2021
Go extract error codes

go-extract-error-codes Overview This library helps to extract possible error codes from configured go-restful applications quality level: PoC High Lev

AccelByte Inc 0 Nov 24, 2021