Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A package that allows you to use try/catch block in Go.

License

NotificationsYou must be signed in to change notification settings

ez4o/go-try

Repository files navigation

ContributorsForksStargazersMIT LicenseIssuesIssues Closed


go-try


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

About

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!

(back to top)

Whygo-try?

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!

(back to top)

Getting Started

Installation

go get github.com/ez4o/go-try

Usage

Try(func () {...ThrowOnError(ce)...ThrowOnError(err)...}).Catch(func (ceCustomError) {...}).Catch(func (eerror,st*StackTrace) {...st.Print()})

Functions

NameDescription
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().

Example

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!

(back to top)

Roadmap

  • Implement catching errors of different types.
  • Tests
  • CI

See theopen issues for a full list ofproposed features (and known issues).

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feat/amazing-feature)
  3. Commit your Changes withConventional 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. SeeLICENSE for moreinformation.

(back to top)

Contact

Author

Project Link

(back to top)

About

A package that allows you to use try/catch block in Go.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp