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

Overview


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 use try/catch block in Go.

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

(back to top)

Why go-try?

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, then go-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 (ce CustomError) {
  ...
}).Catch(func (e error, st *StackTrace) {
  ...
  st.Print()
})

Functions

Name Description
Try() Takes func (), wrap your code here!
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().
ThrowOnError() Takes any. Will only throw an error when the parameter is not nil.
st.Print() If you have declared the second parameter st *StackTrace, you can print the stack trace using st.Print().

Example

Let's say you want to fetch JSON from a url and unmarshal it, you can simply write it like this:

import (
  "encoding/json"
  "fmt"
  "io/ioutil"
  "net/http"
    
  . "github.com/ez4o/go-try"
)

func main() {
  Try(func() {
    resp, err := http.Get("https://jsonplaceholder.typicode.com/posts")
    ThrowOnError(err)
    defer resp.Body.Close()
    
    b, err := ioutil.ReadAll(resp.Body)
    ThrowOnError(err)

    var data []map[string]interface{}
    err = json.Unmarshal(b, &data)
    ThrowOnError(err)

    fmt.Println(data)
  }).Catch(func(e error, st *StackTrace) {
    fmt.Println(e)
    st.Print()
  })
}

For more examples, head over to https://github.com/ez4o/go-try/tree/main/.examples!

(back to top)

Roadmap

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

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
I'd like to share random apps in the spare times. Thus, I'm going to try learning some concepts of Go and as much as I can I try to clarify each line.

go-samples I'd like to share random apps in the spare times. Thus, I'm going to try learning some concepts of Go and as much as I can I try to clarify

Mert Simsek 1 Mar 16, 2022
Header Block is a middleware plugin for Traefik to block request and response headers which regex matched by their name and/or value

Header Block is a middleware plugin for Traefik to block request and response headers which regex matched by their name and/or value Conf

null 3 May 24, 2022
Node is where client will send data to, create block send to miner, create block send to parent and receive tick from validator and do validate

Node Receive Tick from validator Validate POH of tick send result to validator Receive confirm block from validator Send Checked block to validator Ho

Hieu Phan 0 Dec 31, 2021
Go-block-api - Golang implementation of Ethereum Block API

Go Ethereum Block API Golang implementation of Ethereum Block API This API can s

zgur.ETH 1 Jan 13, 2022
Block-top - Tool to watch block production

block top Continuously watch the block production from one or many (or all) lead

Triton One 1 Jan 24, 2022
Catch breakouts by opening positions based on the previous day's range. Popularized by Larry Williams.

Simple Cryptocurrency Breakout Strategy Catch breakouts by opening positions based on the previous day's range. Popularized by Larry Williams. Strateg

Kurtis Streutker 20 Jun 8, 2022
Scanner to send specially crafted requests and catch callbacks of systems that are impacted by Log4J Log4Shell vulnerability (CVE-2021-44228)

scan4log4shell Scanner to send specially crafted requests and catch callbacks of systems that are impacted by Log4J Log4Shell vulnerability CVE-2021-4

Frank Hübner 11 Feb 27, 2022
This testing tool surrounds go-ethereum with cannon to catch the blocks of retesteth going into go-ethereum and test cannon with them

Siege This testing tool surrounds go-ethereum with cannon to catch the blocks of retesteth going into go-ethereum and test cannon with them. Usage Sta

Diederik Loerakker 8 Mar 15, 2022
Fishfindr - An application that is being developed to help me figure out the best places to fish based on my catch history

fishfindr Fishfindr is an application that is being developed to help me figure out the best places to fish based on my catch history. The front-end p

Jake Correnti 2 Feb 10, 2022
A block parser tool that allows extraction of various data types on DAS

das-database A block parser tool that allows extraction of various data types on DAS (register, edit, sell, transfer, ...) from CKB Prerequisites Ubun

DAS 13 Jun 23, 2022
This is a web project in golang, where we will try to use multiple concepts

This is a web project in golang, where we will try to use multiple concepts ##List of all the dependencies we have used in this project Name GoCommand

Arpit Srivastava 1 Feb 10, 2022
PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. You can use PHP like functions in your app, module etc. when you add this module to your project.

PHP Functions for Golang - phpfuncs PHP functions implementation to Golang. This package is for the Go beginners who have developed PHP code before. Y

Serkan Algur 48 Jun 4, 2022
Fleex allows you to create multiple VPS on cloud providers and use them to distribute your workload.

Fleex allows you to create multiple VPS on cloud providers and use them to distribute your workload. Run tools like masscan, puredns, ffuf, httpx or anything you need and get results quickly!

null 149 Jun 19, 2022
Fleex allows you to create multiple VPS on cloud providers and use them to distribute your workload.

Fleex allows you to create multiple VPS on cloud providers and use them to distribute your workload. Run tools like masscan, puredns, ffuf, httpx or a

null 149 Jun 19, 2022
A Docker image that allows you to use Hetzner DNS as a DynDNS Provider

Docker Hetzner DDNS This Docker image will allow you to use the Hetzner DNS Service as a Dynamic DNS Provider (DDNS). How does it work? The Go script

Matthias Kutz 6 Jun 7, 2022
notion-md-gen allows you to use Notion as a CMS for pages built with any static site generators

notion-md-gen allows you to use Notion as a CMS for pages built with any static site generators

Bonaysoft 58 Jun 5, 2022
Allows you to use the magic remote on your webOS LG TV as a keyboard/mouse for your Linux machine

magic4linux Allows you to use the magic remote on your webOS LG TV as a keyboard/mouse for your PC Linux machine. This is a Linux implementation of th

Mathias Fredriksson 0 Feb 7, 2022
Package fsm allows you to add finite-state machines to your Go code.

fsm Package fsm allows you to add finite-state machines to your Go code. States and Events are defined as int consts: const ( StateFoo fsm.State =

Cocoon Space 22 Jun 19, 2022
Felix Geisendörfer 28 Feb 9, 2022
This Go package allows you to set handler functions that run when named events occur

This Go package allows you to set handler functions that run when named events occur

James 1 Feb 10, 2022
Example used to try a compose application with Docker Dev Environments

compose-dev-env Example used to try a Compose application with Docker Dev Environments. This example is based on the nginx-golang-mysql sample of awes

Docker Samples 64 Jun 16, 2022
This project used to learn golang and try to bypass AV

sucksAV This project used to learn golang and try to bypass AV 描述 基于Golang开发的BypassAV,采取的shellcode分离技术,将shellcode注入到图片中,通过加载器进行加载,使用Golang动态加载技术 需要使用第

Crispr 14 May 17, 2022
Trying to build an Ecommerce Microservice in Golang and Will try to make it Cloud Native - Learning Example extending the project of Nic Jackson

Golang Server Project Best Practices Dependency Injection :- In simple words, we want our functions and packages to receive the objects they depend on

Ujjwal Sharma 17 Jun 13, 2022
TinyHat.Me: Microservices deployed with Kubernetes that enable users to propose hat pictures and try on hats from a user-curated database.

Click here to see the "buggy" version ?? The Scenario TinyHat.Me is an up and coming startup that provides an API to allow users to try on tiny hats v

Bit Project 5 Jun 17, 2022
Being played at The Coffee House and try to find and play it on Spotify

The Coffee House Muzik Follow the music that is being played at The Coffee House and try to find and play it on Spotify. Installation Clone this proje

SangND 4 May 25, 2022
Simple code just to try out and Binary Tree on Golang.

Character counter | ▮▮▮▮▮▮▮▮ Simple code just to try out and Binary Tree on Golang. Count characters to train openning a file and reading it, as well

Arthur Abeilice 0 May 17, 2022
Small proof of concept project to try temporal.io with Dispatch Incident Management from Netflix.

temporal-dispatch-poc Small POC project to try out the Temporal workflow engine together with Netflix's Dispatch Incident Management System. Supported

Jørgen 1 Nov 12, 2021