{"id":18451884,"url":"https://github.com/ez4o/go-try","last_synced_at":"2025-04-08T02:32:58.606Z","repository":{"id":57650133,"uuid":"446535411","full_name":"ez4o/go-try","owner":"ez4o","description":"A package that allows you to use try/catch block in Go.","archived":false,"fork":false,"pushed_at":"2022-01-12T18:36:16.000Z","size":13,"stargazers_count":52,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-06-20T16:46:59.457Z","etag":null,"topics":["catch","ez4o","go","golang","try","try-catch"],"latest_commit_sha":null,"homepage":"https://github.com/ez4o/go-try","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ez4o.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-01-10T18:19:47.000Z","updated_at":"2024-06-18T22:43:45.000Z","dependencies_parsed_at":"2022-09-08T23:50:58.649Z","dependency_job_id":null,"html_url":"https://github.com/ez4o/go-try","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ez4o%2Fgo-try","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ez4o%2Fgo-try/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ez4o%2Fgo-try/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ez4o%2Fgo-try/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ez4o","download_url":"https://codeload.github.com/ez4o/go-try/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223300388,"owners_count":17122592,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["catch","ez4o","go","golang","try","try-catch"],"created_at":"2024-11-06T07:29:43.697Z","updated_at":"2024-11-06T07:29:50.293Z","avatar_url":"https://github.com/ez4o.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"\u003cdiv id=\"top\"\u003e\u003c/div\u003e\n\n\u003c!-- PROJECT SHIELDS --\u003e\n\n[\u003cdiv align=\"center\"\u003e ![Contributors][contributors-shield]][contributors-url]\n[![Forks][forks-shield]][forks-url] [![Stargazers][stars-shield]][stars-url]\n[![MIT License][license-shield]][license-url]\n[![Issues][issues-shield]][issues-url]\n[![Issues Closed][issues-closed-shield]\u003c/div\u003e][issues-closed-url]\n\n\u003c!-- ![Visitors](https://estruyf-github.azurewebsites.net/api/VisitorHit?user=wst24365888\u0026repo=ez4o/go-try\u0026countColor=rgb(0,%20126,%20198)) --\u003e\n\n\u003cbr /\u003e\n\n![go-try](https://socialify.git.ci/ez4o/go-try/image?description=1\u0026font=KoHo\u0026name=1\u0026owner=1\u0026pattern=Circuit%20Board\u0026theme=Light)\n\n\u003cbr /\u003e\n\u003cdiv align=\"center\"\u003e\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/ez4o/go-try#usage\"\u003e\u003cstrong\u003eExplore Usage »\u003c/strong\u003e\u003c/a\u003e\n    \u003cbr /\u003e\n    \u003cbr /\u003e\n    \u003ca href=\"https://github.com/ez4o/go-try/issues\"\u003eReport Bug\u003c/a\u003e\n    ·\n    \u003ca href=\"https://github.com/ez4o/go-try/issues\"\u003eRequest Feature\u003c/a\u003e\n  \u003c/p\u003e\n\u003c/div\u003e\n\n\u003cdetails\u003e\n  \u003csummary\u003eTable of Contents\u003c/summary\u003e\n  \u003col\u003e\n    \u003cli\u003e\u003ca href=\"#about\"\u003eAbout\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"#why-go-try\"\u003eWhy go-try?\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\n      \u003ca href=\"#getting-started\"\u003eGetting Started\u003c/a\u003e\n      \u003cul\u003e\n        \u003cli\u003e\u003ca href=\"#usage\"\u003eUsage\u003c/a\u003e\u003c/li\u003e\n        \u003cli\u003e\u003ca href=\"#example\"\u003eExample\u003c/a\u003e\u003c/li\u003e\n      \u003c/ul\u003e\n    \u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"#roadmap\"\u003eRoadmap\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"#contributing\"\u003eContributing\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"#license\"\u003eLicense\u003c/a\u003e\u003c/li\u003e\n    \u003cli\u003e\u003ca href=\"#contact\"\u003eContact\u003c/a\u003e\u003c/li\u003e\n  \u003c/ol\u003e\n\u003c/details\u003e\n\n## About\n\n`go-try` is a package that allows you to use `try/catch` block in Go.\n\nThis project is still in its early stages, so any thoughts and feedback are very\nwelcome!\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n## Why `go-try`?\n\nMany Go developers get tired of dealing with errors because there are too many\nerrors to handle one by one, which is intuitive and effective, but really\nannoying.\n\nI've been trying to find out if Go has an error handling method like\n`try/catch`, I think that would probably be a lot easier, but unfortunately, I\ncouldn't find any package that's easy to use.\n\nSo I tried to make one myself, taking inspiration from the `try/catch` syntax,\nthen `go-try` was born!\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n## Getting Started\n\n### Installation\n\n`go get github.com/ez4o/go-try`\n\n### Usage\n\n```go\nTry(func () {\n  ...\n  ThrowOnError(ce)\n  ...\n  ThrowOnError(err)\n  ...\n}).Catch(func (ce CustomError) {\n  ...\n}).Catch(func (e error, st *StackTrace) {\n  ...\n  st.Print()\n})\n```\n\n### Functions\n\n| Name             | Description                                                                                                                                                                                                |\n| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `Try()`          | Takes `func ()`, wrap your code here!                                                                                                                                                                      |\n| `Catch()`        | Takes `func (any)` or `func (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 last `ThrowOnError()`. |\n| `ThrowOnError()` | Takes `any`. **Will only throw an error when the parameter is not** `nil`.                                                                                                                                 |\n| `st.Print()`     | If you have declared the second parameter `st *StackTrace`, you can print the stack trace using `st.Print()`.                                                                                              |\n\n### Example\n\nLet's say you want to fetch JSON from a url and unmarshal it, you can simply\nwrite it like this:\n\n```go\nimport (\n  \"encoding/json\"\n  \"fmt\"\n  \"io/ioutil\"\n  \"net/http\"\n    \n  . \"github.com/ez4o/go-try\"\n)\n\nfunc main() {\n  Try(func() {\n    resp, err := http.Get(\"https://jsonplaceholder.typicode.com/posts\")\n    ThrowOnError(err)\n    defer resp.Body.Close()\n    \n    b, err := ioutil.ReadAll(resp.Body)\n    ThrowOnError(err)\n\n    var data []map[string]interface{}\n    err = json.Unmarshal(b, \u0026data)\n    ThrowOnError(err)\n\n    fmt.Println(data)\n  }).Catch(func(e error, st *StackTrace) {\n    fmt.Println(e)\n    st.Print()\n  })\n}\n```\n\nFor more examples, head over to\n\u003chttps://github.com/ez4o/go-try/tree/main/.examples\u003e!\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n## Roadmap\n\n- [x] Implement catching errors of different types.\n- [ ] Tests\n- [ ] CI\n\nSee the [open issues](https://github.com/ez4o/go-try/issues) for a full list of\nproposed features (and known issues).\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n## Contributing\n\nContributions are what make the open source community such an amazing place to\nlearn, inspire, and create. Any contributions you make are **greatly\nappreciated**.\n\nIf you have a suggestion that would make this better, please fork the repo and\ncreate a pull request. You can also simply open an issue with the tag\n\"enhancement\". Don't forget to give the project a star! Thanks again!\n\n1. Fork the Project\n2. Create your Feature Branch (`git checkout -b feat/amazing-feature`)\n3. Commit your Changes with\n   [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/)\n4. Push to the Branch (`git push origin feat/amazing-feature`)\n5. Open a Pull Request\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n## License\n\nDistributed under the MIT License. See\n[LICENSE](https://github.com/ez4o/go-try/blob/main/LICENSE) for more\ninformation.\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n## Contact\n\n### Author\n\n- HSING-HAN, WU (Xyphuz)\n  - Mail me: xyphuzwu@gmail.com\n  - About me: \u003chttps://about.xyphuz.com\u003e\n  - GitHub: \u003chttps://github.com/wst24365888\u003e\n\n### Project Link\n\n- \u003chttps://github.com/ez4o/go-try\u003e\n\n\u003cp align=\"right\"\u003e(\u003ca href=\"#top\"\u003eback to top\u003c/a\u003e)\u003c/p\u003e\n\n\u003c!-- MARKDOWN LINKS \u0026 IMAGES --\u003e\n\u003c!-- https://www.markdownguide.org/basic-syntax/#reference-style-links --\u003e\n\n[contributors-shield]: https://img.shields.io/github/contributors/ez4o/go-try.svg?style=for-the-badge\n[contributors-url]: https://github.com/ez4o/go-try/graphs/contributors\n[forks-shield]: https://img.shields.io/github/forks/ez4o/go-try.svg?style=for-the-badge\n[forks-url]: https://github.com/ez4o/go-try/network/members\n[stars-shield]: https://img.shields.io/github/stars/ez4o/go-try.svg?style=for-the-badge\n[stars-url]: https://github.com/ez4o/go-try/stargazers\n[issues-shield]: https://img.shields.io/github/issues/ez4o/go-try.svg?style=for-the-badge\n[issues-url]: https://github.com/ez4o/go-try/issues\n[issues-closed-shield]: https://img.shields.io/github/issues-closed/ez4o/go-try.svg?style=for-the-badge\n[issues-closed-url]: https://github.com/ez4o/go-try/issues?q=is%3Aissue+is%3Aclosed\n[license-shield]: https://img.shields.io/github/license/ez4o/go-try.svg?style=for-the-badge\n[license-url]: https://github.com/ez4o/go-try/blob/main/LICENSE\n[product-screenshot]: https://go-try.ez4o.com/?username=wst24365888\u0026img_url=https%3A%2F%2Fimages.unsplash.com%2Fphoto-1506744038136-46273834b3fb%3Fixid%3DMnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8%26ixlib%3Drb-1.2.1%26auto%3Dformat%26fit%3Dcrop%26w%3D1000%26q%3D80\u0026fbclid=IwAR1AUDKHzjzBSjKle6J44dYRSrIbvBu8eTxtrfhpPxhBnBsOizgSq63bYbU\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fez4o%2Fgo-try","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fez4o%2Fgo-try","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fez4o%2Fgo-try/lists"}