- Notifications
You must be signed in to change notification settings - Fork3
A package that allows you to use try/catch block in Go.
License
ez4o/go-try
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
go-try
is a package that allows you to usetry/catch
block in Go.
This project is still in its early stages, so any thoughts and feedback are verywelcome!
Many Go developers get tired of dealing with errors because there are too manyerrors to handle one by one, which is intuitive and effective, but reallyannoying.
I've been trying to find out if Go has an error handling method liketry/catch
, I think that would probably be a lot easier, but unfortunately, Icouldn't find any package that's easy to use.
So I tried to make one myself, taking inspiration from thetry/catch
syntax,thengo-try
was born!
go get github.com/ez4o/go-try
Try(func () {...ThrowOnError(ce)...ThrowOnError(err)...}).Catch(func (ceCustomError) {...}).Catch(func (eerror,st*StackTrace) {...st.Print()})
Name | Description |
---|---|
Try() | Takesfunc () , wrap your code here! |
Catch() | Takesfunc (any) orfunc (any, *StackTrace) , and it will only accept the error type you have declared. You can accept second parameter, which is the stack trace begin from the lastThrowOnError() . |
ThrowOnError() | Takesany .Will only throw an error when the parameter is notnil . |
st.Print() | If you have declared the second parameterst *StackTrace , you can print the stack trace usingst.Print() . |
Let's say you want to fetch JSON from a url and unmarshal it, you can simplywrite it like this:
import ("encoding/json""fmt""io/ioutil""net/http" ."github.com/ez4o/go-try")funcmain() {Try(func() {resp,err:=http.Get("https://jsonplaceholder.typicode.com/posts")ThrowOnError(err)deferresp.Body.Close()b,err:=ioutil.ReadAll(resp.Body)ThrowOnError(err)vardata []map[string]interface{}err=json.Unmarshal(b,&data)ThrowOnError(err)fmt.Println(data) }).Catch(func(eerror,st*StackTrace) {fmt.Println(e)st.Print() })}
For more examples, head over tohttps://github.com/ez4o/go-try/tree/main/.examples!
- Implement catching errors of different types.
- Tests
- CI
See theopen issues for a full list ofproposed features (and known issues).
Contributions are what make the open source community such an amazing place tolearn, inspire, and create. Any contributions you make aregreatlyappreciated.
If you have a suggestion that would make this better, please fork the repo andcreate 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 withConventional Commits
- Push to the Branch (
git push origin feat/amazing-feature
) - Open a Pull Request
Distributed under the MIT License. SeeLICENSE for moreinformation.
- HSING-HAN, WU (Xyphuz)
- Mail me:xyphuzwu@gmail.com
- About me:https://about.xyphuz.com
- GitHub:https://github.com/wst24365888