https://github.com/apperia-de/goroutine
A goroutine wrapper for creating and running panic safe goroutines.
https://github.com/apperia-de/goroutine
goroutine panic safe wrapper
Last synced: about 1 month ago
JSON representation
A goroutine wrapper for creating and running panic safe goroutines.
- Host: GitHub
- URL: https://github.com/apperia-de/goroutine
- Owner: apperia-de
- License: mit
- Created: 2021-07-28T17:47:53.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-08-08T10:34:07.000Z (over 4 years ago)
- Last Synced: 2026-01-14T19:09:22.702Z (about 1 month ago)
- Topics: goroutine, panic, safe, wrapper
- Language: Go
- Homepage:
- Size: 47.9 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README

# goroutine
[](https://goreportcard.com/report/github.com/sknr/goroutine)


A goroutine wrapper for creating and running panic safe goroutines.
The purpose of this package is to provide a simple wrapper function for goroutines, which automatically handles panics
in goroutines. Starting a new goroutine without taking care of recovering from a possible panic in that goroutine itself
could crash the whole application.
The `Go` function runs an arbitrary function `f` in a separate goroutine, which handles the recovering from panic within
that goroutine.
## Installation
`go get -u github.com/sknr/goroutine`
## Usage (with dot import)
Instead of running
```
go func() {
panic("Panic raised in goroutine")
}()
```
simply call
```
Go(func() {
panic("Panic raised in goroutine")
})
```
in order to create a panic safe goroutine.
Functions with multiple input params must be wrapped within an anonymous function.
Instead of running
```
go func(a, b int) {
panic(a+b)
}(21,21)
```
simply call
```
Go(func() {
func(a, b int) {
panic(a+b)
}(21,21)
})
```
## Examples
```
package main
import (
"fmt"
"github.com/sknr/goroutine"
"log"
)
func init() {
// Override the default recover function.
goroutine.SetDefaultRecoverFunc(func(v interface{}, done chan<- error) {
log.Printf("%v", v)
done <- fmt.Errorf("panic in goroutine successfully recovered")
})
}
func main() {
for i := -3; i <= 3; i++ {
err := <-goroutine.Go(func() {
func(a, b int) {
log.Println(a, "/", b, "=", a/b)
}(10, i)
})
if err != nil {
log.Println(err)
}
}
}
```
### Set a new default recover function
In order to override the default recover function for new goroutines (created with `Go(func())` or `New(func())`),
simply set a custom recover function of type `RecoverFunc` with `SetDefaultRecoverFunc`
### Create a new Goroutine instance with a custom recover function
If you need different recover functions for different goroutines, you can simply call
```
goroutine.New(func() {
func(name string) {
panic(fmt.Sprintln("Hallo", name))
}("Welt")
}).WithRecoverFunc(func(v interface{}, done chan<- error) {
log.Printf("Custom recover function in goroutine, with error: %v", v)
}).Go()
```
## Logo
- Gopher by [Gophers](https://github.com/egonelbre/gophers)