Skip to content

Latest commit

 

History

History
69 lines (55 loc) · 2.64 KB

README.md

File metadata and controls

69 lines (55 loc) · 2.64 KB

A library for safely using go-routine.


elliotxx/safe is a library for safely using go-routine, inspired by safe!

📜 Language

English | 简体中文

⚡ Usage

go get -u github.com/elliotxx/safe

📚 Examples

package main

import "github.com/elliotxx/safe"

func doSomething() {
	panic("BOOM!")
}

func customRecoverHandler(r interface{}) {
	log.Printf("Recovered from %v", r)
}

func main() {
	// Go starts a recoverable goroutine.
	safe.Go(
		doSomething,
	)

	// GoR starts a recoverable goroutine using given recover handler.
	safe.GoR(
		doSomething,
		customRecoverHandler
	)

	// DefaultHandleCrash simply catches a crash with the default recover handler.
	go func() {
		defer safe.DefaultHandleCrash()
		doSomething()
	}()

	// HandleCrash catches a crash with the custom recover handlers.
	go func() {
		defer safe.HandleCrash(customRecoverHandler)
		doSomething()
	}()
}