Table of Contents
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!
go-ez
?
Why 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!
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.
Getting Started
Installation
go get github.com/ez4o/go-ez
Usage
-
Simply pass anything to
Got[T, U]()
. Remember to specify input type and output type. -
Write what you want to do in
Then()
. Wrap each of them with the functions below. -
Handle the returned pair of
(value, error)
fromGot(...).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)
}
Roadmap
- Handle functions that have no parameters but return values.
See the open issues for a full list of proposed features (and known issues).
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!
- Fork the Project
- Create your Feature Branch (
git checkout -b feat/amazing-feature
) - Commit your Changes with Conventional Commits
- Push to the Branch (
git push origin feat/amazing-feature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE for more information.
Contact
Author
- HSING-HAN, WU (Xyphuz)
- Mail me: [email protected]
- About me: https://about.xyphuz.com
- GitHub: https://github.com/wst24365888