Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ghosind/go-try
The try...catch...finally statement alternative in Golang.
https://github.com/ghosind/go-try
go-library go-package go-try-catch try-catch try-catch-wrap
Last synced: 9 days ago
JSON representation
The try...catch...finally statement alternative in Golang.
- Host: GitHub
- URL: https://github.com/ghosind/go-try
- Owner: ghosind
- License: mit
- Created: 2024-04-03T13:37:14.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-04-12T13:19:17.000Z (7 months ago)
- Last Synced: 2024-06-19T13:43:36.349Z (5 months ago)
- Topics: go-library, go-package, go-try-catch, try-catch, try-catch-wrap
- Language: Go
- Homepage: https://pkg.go.dev/github.com/ghosind/go-try
- Size: 16.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-try
![test](https://github.com/ghosind/go-try/workflows/test/badge.svg)
[![Go Report Card](https://goreportcard.com/badge/github.com/ghosind/go-try)](https://goreportcard.com/report/github.com/ghosind/go-try)
[![codecov](https://codecov.io/gh/ghosind/go-try/branch/main/graph/badge.svg)](https://codecov.io/gh/ghosind/go-try)
![Version Badge](https://img.shields.io/github/v/release/ghosind/go-try)
![License Badge](https://img.shields.io/github/license/ghosind/go-try)
[![Go Reference](https://pkg.go.dev/badge/github.com/ghosind/go-try.svg)](https://pkg.go.dev/github.com/ghosind/go-try)The `try...catch` statement alternative in Golang.
## Installation
```
go get -u github.com/ghosind/go-try
```## Getting Started
There is the simplest example to run a function and handle the error in the `catch` function that the `try` function returned.
```go
out, err := try.TryCatch(func () (error) {
// Do something
return err
}, func (err error) {
// The function will not executing if err is nil.
// Handle error...
})
```The try functions will return two values. The first value is the all values the `try` function returned, and the second value is the error that the `try` function returns or it panics.
```go
out, err := try.Try(func () (string, error) {
return "Hello world", errors.New("expected error")
})
fmt.Println(out)
fmt.Println(err)
// [Hello world, expected error]
// expected error
```The package provides four forms for the `try` statement alternative:
- `Try`
- `TryFinally`
- `TryCatch`
- `TryCatchFinally`In default cases, the try functions will catch the error that the `try` function panics, and you can set the `CatchPanic` variable to `false` to disable it.
```go
try.CatchPanic = false
try.Try(func () {
panic("panic error")
})
// panic: expected error
```